Having difficulty locating an element using ID, XPATH and CSS - selenium-webdriver

Unable to locate an anchor element present inside an li element using Selenium webdriver.
For the below HTML DOM structure, I am unable to locate the second li element. I basically want to locate it and perform click operation on it there by loading in a new page using Selenium Webdriver.
Tried to access it using the below methods:
id - docTab
xpath -.//div[#class='secondary-tabs']/ul/li[1]/a
xpath - //*[#id="docTab"]
xpath - /html/body/div[1]/main/div[2]/div/div/div[2]/div[1]/ul/li[2]/a
css - #docTab. tab__heading
css - li[id="docTab"]
But all of it are resulting in "Unable to locate element" error. This is happening even after including driver.wait methods.
DOM Structure:
<div class="secondary-tabs">
<ul class="tabs tabs--bordered">
<li class="tab active second_tabs" id="textTab" tabdivid="textSecond"><a><div class="tab__heading" title="Text">Text</div></a></li>
<li class="tab second_tabs" id="docTab" tabdivid="docSecond"><a><div class="tab__heading" title="File">Document</div></a></li>
<li class="tab second_tabs" id="faqTab" tabdivid="faqSecond"><a><div class="tab__heading" title="FAQ" onclick="FAQ">FAQ</div></a></li>
</ul>
</div>
I expect to locate the second li element and perform click event on it using selenium web driver.

To click() on the element with text as Document you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Java:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.secondary-tabs>ul li#docTab[tabdivid='docSecond']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondary-tabs']/ul//li[#id='docTab' and #tabdivid='docSecond']"))).click();

You can try below given selector.
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='secondarytabs']//child::ul//child::li[#id='docTab' and tabdivid='docSecond']"))).click();

Related

How to locate element in selenium for href

I am trying to locate the element for this:
<a ng-if="showLink && customer.partnerType == 2 && customer.isDirectCustomer" class="cp_text_link ng-binding ng-scope" ng-href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=Attunix" href="/?orgId=77bc101729ad844e39c4c1e17231c7e4&orgName=ABC">
ABC
</a>
I tried the XPath and CssSelector but its unable to locate element.
Can someone pls help me locate the element
TIA
Simply use text:
driver.findElement(By.xpath("//a[contains(text(),'abc')]"));
It is quite hard to come up with an exact locator without seeing the full code of the page, you're trying to automate.
From what I can see so far it makes sense to stick to ABC text so try the following:
Partial Link Text
driver.findElement(By.partialLinkText("ABC"));
Or the equivalent XPath
driver.findElement(By.xpath("//a[contains(text(),'ABC')]"));
Try using class
driver.findElement(By.cssSelector("a.cp_text_link"))
The desired element is an Angular element so to locate and invoke click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Java based Locator Strategies:
partialLinkText:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("ABC"))).click();
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cp_text_link.ng-binding.ng-scope[ng-href*='orgId'][href$='ABC']"))).click();
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='cp_text_link ng-binding ng-scope' and contains(#ng-href, 'orgId')][contains(#href, 'ABC')]"))).click();

How to locate the element using Selenium Java

<input name="txtAnswer" class="box1" id="txtAnswer" type="text" maxlength="20">
My code:
driver.findElement(By.name("txtAnswer")).sendKeys("green");
To locate the desired element you can use either of the following Locator Strategies:
Using cssSelector:
driver.findElement(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']")).sendKeys("green")
Using xpath:
driver.findElement(By.xpath("//input[starts-with(#class, 'box') and #id='txtAnswer'][#name='txtAnswer']")).sendKeys("green")
Update
As you are seeing the error Unable to locate element you need to induce WebDruverWait for the element to be clickable and you can use either of the following solutions:
Using cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#txtAnswer[class^='box'][name='txtAnswer']"))).sendKeys("green")
Using xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[starts-with(#class, 'box') and #id='txtAnswer'][#name='txtAnswer']"))).sendKeys("green")
Id and class are two attributes for an web-element. to identify web element uniquely(one) id is used and to identify web elements use common properties like class, tag name etc..
so writing xpath with id attribute will give you unique match.
Answer: driver.findElement(By.id("txtAnswer")).sendKeys("green");

How do I locate the button element and click it

<button class="sfc-value sfc-js-change sfc-js-field sfc-ui-btn fo-ui-btn fo-ui-container fo-ui-fixed style-114 layout-156 sfc-js-click" type="button">
<span class="sfc-caption fo-ui-label style-115 layout-157 fo-ui-no-icon">Next</span>
</button>
I am new to selenium and I am confused with the classes to target in here. So any best way to target the element .I need to click the button here.
You create an xpath that starts with the span using the text contained, then refer -back- to the button itself:
driver.findElement(By.xpath("//span[text()='Next']/..")).click();
**edited
To desired element is a dynamic element so to target the element and click it you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following (Java) solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.sfc-value.sfc-js-change.sfc-js-field.sfc-ui-btn.fo-ui-btn.fo-ui-container.fo-ui-fixed.style-114.layout-156.sfc-js-click>span.sfc-caption.fo-ui-label.style-115.layout-157.fo-ui-no-icon"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='sfc-value sfc-js-change sfc-js-field sfc-ui-btn fo-ui-btn fo-ui-container fo-ui-fixed style-114 layout-156 sfc-js-click']/span[contains(text(),'Next')]"))).click();

Unable to click on a link

I have been trying to click on a link in my application at work , but the control failed to click on the link whichever locator I used. I test all the locators with Selenium IDE and javascript before using them in the script and it all seem fine prior to running the test. I manage to identify and click on the link with this:
#FindBy(css ="/html/body/div[4]/div/div/div[2]/div[1]/a/strong") but I ran the test the second time but it failed to click on the link same.
See below the html code:
<div class="container container-outer">
<div class="row-fluid long-text-fitted content-container" id="page-content-container">
<div class="span12">
<div class="row-fluid container-alert">
</div>
<div class="left-side-spacer-layout right-side-spacer-layout" id="page-content">
<div class="button-spacer" id="breadcrumb-content">
<a href="/shopping/marketplace/landingPage">
<strong>< Back to Search Results</strong>
</a>
You should click on parameter. remove strong attribute.
/#FindBy(css ="/html/body/div[4]/div/div/div[2]/div[1]/a")
I would avoid using XPath in general and especially an XPath that starts at the HTML tag. That makes the test very fragile. I would suggest a CSS selector like the below.
driver.findElement(By.cssSelector("#breadcrumb-content > a")).click();
The CSS selector reads find an A tag that is a child (>) of an element with an ID (#) of breadcrumb-content.
Read more about CSS Selectors.
Using absolute Xpath is not a good idea. As you said it is working for the first time then possible your DOM strcture is changing after click it once
You can try with below xpath:
driver.findElement(By.xpath("//strong[contains(.,'< Back to Search Results')]")).click();
If still not working, you should try to click using JavascriptExecutor as it directly working on HTML DOM. Feel free to locate element in below code according to your convenience:
WebElement element=driver.findElement(By.xpath("//strong[contains(.,'< Back to Search Results')]"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Runtime.evaluate threw exception: Error: element is not attached to the page document error when clicking on a element using watir-webdriver

HTML Tags
<li class="filter-categ">
<ul class="l-h-list">
<li id="filter__entityBased" class="item filter_value" filter-name="entityBased" search- name="zone/concept/store_based">Zone/Concept/Store based</li>
<li id="entityBased" class="sprite-filter all-filter"></li>
</ul>
</li>
I am trying to click on the element with id=entityBased, but I am getting the error."Error: element is not attached to the page document"
Same works fine in Firefox and IE. Problem is seen only in Chrome.
I am using the following code to perform the action.
browser.element(:id => "entityBased").wait_until_present
browser.element(:id => "entityBased").click
My Environment Details
Chrome Version 29.0.1547.66 m
Watir-WebDriver (0.6.4, 0.6.2)
watir (4.0.2 ruby x86-mingw32)
This is blockin my automation in Chrome. Please help me out in this problem.
I can't comment, sorry.
Have you tried testing with a 5 second sleep, instead of wait until present? From what I could tell, this generally occurs when the javascript reloads the element after the page has loaded.
user2783685
I had the same problem and I found solution in
stale element reference: element is not attached to the page document
So basically javascript reloading element and even I have this on your dom, driver thinking that it's detached.
and you need to catch org.openqa.selenium.StaleElementReferenceException
may be there is another way, I don't know.

Resources