how to click two identical attributes with Selenium WebDriver? - java

In firepath I saw two identical attributes, firepath has two results.
Here is the highlighted HTML code below in firebug:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
And below is the whole code:
<button class="list_header_search_toggle icon-search btn btn-icon table-btn-lg" style="margin-left:0px">
<span class="sr-only">Search</span>
</button>
NOTE: There is only 1 search button, I search it every where and there is only 1 but it shows two??
How to code this in selenium web driver?
The snippet from firepath:
Update:
Html code image, from firepath:

You can use XPath functions, for example:
position() returns the position of element at DOM
//button[#id='hdr_problem_task']/th[2]/button[position()=1]
last()
//button[#id='hdr_problem_task']/th[2]/button[last()]
something like first() doesn't exist, instead of this you can use index:
//button[#id='hdr_problem_task']/th[2]/button[1]
Also if button has some text you can use it as well:
//button[#id='hdr_problem_task']/th[2]/button[text()='button name']
or with contains()
//button[#id='hdr_problem_task']/th[2]/button[contains(text(), 'button name')]
UPDATE:
The button has name Search you can use XPath with - contains().
One more small suggestion, don't forget about future support. And instead of the following locator:
//*[#id='hdr_problem_task']/th[2]/button
Much better will be:
//button[#id='hdr_problem_task']/th[2]/button

You can use th tag's name attribute value in order to recognize the correct Search button, as shown below:
//th[#name='search'][1]/button/span[text()='Search']
Let me know, whether it works for you.

Related

Selenium skip html element with hidden attribute

I'm using selenium with java and I'm trying to test the search scenario. After I perform the search the html elements which do not suite the search keyword are hidden with hidden attribute (see the example below, the first element does not match the search criteria and the second does):
<ion-item-sliding class="item-wrapper" hidden="">
<button class="item item-block item-md" ion-item="">
<p>I am hidden</p>
</button>
</ion-item-sliding>
<ion-item-sliding class="item-wrapper">
<button class="item item-block item-md" ion-item="">
<p>I am not</p>
</button>
</ion-item-sliding>
My goal is to find the text in the visible element (the second one in the example). When I use the simple selector
//button[#class='item item-block item-md']
the hidden element gets found, so I'm using the selector like this
//ion-item-sliding[#class='item-wrapper' and not
#type='hidden']//button[#class='item item-block item-md']...
but no luck. Please advice with any ideas/documentation about the selector.
What you need here is to utilize the getAttribute function. What this call does is that it returns an attribute value if its set or null otherwise.
You can use FindElements passing your selector, and iterate over the found elements and only return the one where
getAttribute("hidden") != null
Hope this helps.
Use isDisplayed() method to see if the element is visible on the browser before getting the text, like this
List <WebElement> elements = driver.findElements(By.cssSelector(".item.item-block.item-md"));
for(WebElement e:elements)
{
if(e.isDisplayed())
String text = e.getAttribute("innerText");
}

How can i click on this button with selenium

How can i click on this button with selenium ?
<a class="_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy" role="button" href="" ajaxify="/nux/wizard/step/?action=skip" rel="async-post" id="u_9_8">İleri</a>
Something I wish I would have figured out earlier was how to create my own advanced CSS selectors here is the page that taught me, it will work in all cases assuming your element is visible in the DOM.
https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/
For your given element you could write this many ways
Generic form
tag[attribute='ATTRIBUTE_VALUE']
For your example
a[id='u_9_8']
or
a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']
or
a[rel='async-post']
Now all these selectors will only be useful if the attribute is unique. But take a look at that article there are many tricks you can use to make CSS selectors work for you.
By using xpath with contains text you can click on the element(below is the answer)
driver.findElement(By.xpath("//a[contains(text(),'Ileri')]")).click();
Try it and let me know if it works for you
Try any of these below mentioned code.
Using id locator
driver.findElement(By.id("u_9_8")).click();
Using xpath locator
driver.findElement(By.xpath("//a[text()= 'İleri']").click();
Explanation:- Use text method along with <a>tag.
driver.findElement(By.xpath("//a[#role='button'][text()= 'İleri']").click();
Explanation:- Use role attribute and text method along with <a> tag.
Please add the wait conditions before you are going to click
Element clicking Via linkText :
webDriver.findElement(By.linkText("İleri")).click();
Element clicking Via id :
webDriver.findElement(By.id("u_9_8")).click();
Element clicking Via cssSelector :
1.webDriver.findElement(By.cssSelector("._42ft._4jy0.rfloat._ohf._4jy4._517h._51sy")).click();
2.webDriver.findElement(By.cssSelector("a[class='_42ft _4jy0 rfloat _ohf _4jy4 _517h _51sy']")).click();
Element clicking Via javaScript :
((JavascriptExecutor) driver).executeScript("arguments[0].click();", webElement);
Here you need to pass the element locator instead of webElement.
We can see ID attribute tag so we can use ID "u_9_8" to click on the button.
use the below code.
driver.findelement(By.id("u_9_8")).click();
I think you should be able to use the id
driver.findElement(By.id("u_9_8")).click();
Give it a shot

#FindAll Is Kind of Slow When Trying to Locate Multiple Elements

I have a page that is localized and the "Create Account" WebElement can be English, Chinese or Japanese. I am using Selenium, Java and TestNG framework to run a test to click on this element. However, the slow performance when using this #FindAll to identify the page makes me wonder if there are any better way to do this.
The element from Inspect element while "English" locale is selected:
<div class="form-group">
<a translate="create-account" class="pointer ng-scope" ng-click="vm.createAccount()">Create Account</a>
</div>
My FindAll declaration:
#FindAll({
#FindBy(linkText="Create Account"),
#FindBy(linkText="创建账号"),
#FindBy(linkText="アカウントを作成")
})
private List<WebElement> createAccount;
As a baseline to compare, if I use the #FindAll above, it takes about 15 seconds before Webdriver clicks on the link. If I use just #FindBy, it takes about 2-3 seconds. However, #FindBy does not work for me as I need to be able to locate the correct locale to click on the link.
You could use a single css selector like:
a[ng-click*='createAccount']
Or one of the xpaths:
//a[contains(#ng-click, 'createAccount')]
//a[contains(text(), 'Create Account') or contains(text(), '创建账号') or contains(text(), 'アカウントを作成')]
For css if you pass part of the attribute value then it should be [#attributeName*='part_of_attribute_value']
Please take a look here to view a basic list of css rules w3schools css selectors
Thanks #lauda for helping out and the link to w3 css selectors.
I actually found two more ways that I can identify this link using css:
#FindBy(css="a[translate='create-account']")
private WebElement CreateAccount;
and
#FindBy (css="a.pointer.ng-scope")
private WebElement CreateAccount;
However, not sure why the original solution that lauda posted did not work for me though.
a[ng-click*='createAccount']

Selenium: how to get the value of hidden element which has all div tags

I would like to get the value of all div tags specified in attached. I have tried with all possible locators like classname etc, which is showing null. and tried with JavaScript also which is returning null.
Please see the screen shot and I need the selected text which is in blue color starts with "Enables enterprise IT to deploy networking services"
You need to research creating selectors as this isn't a difficult one. There are numerous approaches for this element, but here's one for you: $$("#offers-popover .description"). Obviously this is a CSS selector based on the $$ and you use getText from the Selenium API in order to scrape the element text, which is what I assume you are intending to do.
driver.findElement(By.css("#offers-popover .description")).getText();
Since your element is not visible you can try this:
String divText = driver.findElement(By.className("description")).getAttribute("textContent");
Or, if this is not the only element on the page with the class description:
WebElement popElement = driver.findElement(By.id("offers-popover"));
String divText = popElement.findElement(By.className("description")).getAttribute("textContent");

How to get Text from this HTML code By using Selenium WebDriver with Java

HTML Code
<label for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21">Royality Free</label>
Selenium Code
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21")).getText();
The above selenium code is not working even i tried getAttribute();
its showing NullPointerException
You are trying to read text from the label but you are finding an element which has id ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 This is not the id of the label.
Your code should be:
WebElement labelElement = driver.findElement(By.cssSelector("label[for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21"]"));
System.out.println(labelElement.getText());
This should work.
Moreover, the locator: ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 seems to be a randomly generated locator. Just confirm that it's not such a case. If it is then you will need to change your locating strategy.

Categories