I am in a situation where there are no unique id and there are number of div's under a class. Cssselector and xpath's are so generic that they are not being recognized.
This is what the Html looks like:
This is my code which doesn't work:
#Test
public void NaviToEpisode(){
driver.findElement(By.linkText("/episode")).click();
title_episode = driver.getTitle();
Assert.assertTrue(title_episode.contains("File uploading"));
}
Please help!
You can use cssSelector, in your case it would be:
driver.findElement(By.cssSelector("#links>div>a").click();
If you use Firefox, install Firebug plugin, then right click on the element you wish to inspect and in menu click on "Inspect with Firebug", once the snippet of your code highlighted right click on it and you should see an option to copy xpath or css.
try this driver.findElement(By.xpath("//*[contains(#href, '/episode/')]")).click();
<div id="links" . . > seems to be static I hope it's unique as well. Following css selector can be used to select first link (i.e. /episodes/)
#links div:nth-child(1) a
Similarly you can use css selectors to select sub-sequent elements. For example to select 2nd element:
#links div:nth-child(2) a
So instead of using By.linkText("/episode"), use By.cssSelector("#links div:nth-child(1) a").
Related
i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();
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
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']
<div class="col-lg-3 col-sm-4 col-xs-6">
<div class="logo">..................</div> </div>
logo is inner div.
I need to acces col-lg-3 col... div content using xpath.
I did this in java:
WebElement mydiv = driver.findElement(By.className("col-lg-3 ")) ;
but it does not work - because it has spaces in the name.
How do I access this using xpath?
In CSS, you could access it like this:
driver.findElement(By.cssSelector(".col-lg-3 .logo"));
Since you asked, in XPath, it could be:
driver.findElement(By.xpath("//div[contains(#class, 'col-lg-3')]/div[contains(#class, 'logo')]")
As you can see, the CSS selector is much simpler.
To see xpath, I suggest to install Firebug and FirePath; those extensions are Firefox.
Example:
Can you see the image? xpath
Imagine that you don't have id neither name locators or you need to use xpath
Open Firefox.
Click on "Firebug". (#1)
There a panel.
Click on "Click an element in the page to inspect" icon (#2)
Click on "FirePath" tab. (#3)
Select the element that you wish (#4)
It displays the xpath into textbox. (#5)
So, you need to add this line:
driver.findElement(By.xpath("html/body/form/fieldset/p[1]/input"));
Here is the HTML:
<li>
<input type="checkbox" checked="" name="selectedMstrPrivGroupList[9].mstrAuthorities[0].status"/>
Add Dexter
</li>
How could this element be clicked in WebDriver? It is a check box. And I want to use XPath as I have close to 30+ check boxes in the page. So that I can create a generic method and pass only the WebElement. I tried the following but didn't work.
Driver.findElement(By.xpath("//input[contains(.,'Add Dexter')]")).click();
If the checkbox next to "Add Dexter" is what you want to click on the page, you can use:
Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[#type='checkbox']")).click();
What is with this one:
Driver.findElement(By.xpath("//input[#name='selectedMstrPrivGroupList[9].mstrAuthorities[0].status']")).click();
You can use like this,
driver.findElement(By.xpath("//li[contains(text(),'Add Dexter')]")).click()
You can use xpath to click on the element as below:
driver.findElement(By.xpath("//input[text()='Add Dexter']")).click();
You can also click on that element by using cssSelector instead of xpath as below:
driver.findElement(By.cssSelector("input:contains(^Add Dexter$)")).click();
Note: CssPath/CssSelector is faster than xpath. So it's better to use cssSelector than xpath in most cases.