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
Related
I am trying to click on a checkbox that only has a name.
Here is the HTML:
<td class="checkbox-column"><input type="checkbox" name="link-active[138]"></td>
Here is my latest attempt to find the element, I have tried a lot of different methods. I also need to grab it without the "[127]" part since that is dynamic in this case.
driver.findElement(By.xpath("//*[text()[contains(.,'link-active')]]")).click();
You are using wrong xpath, correct xpath is:
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
You need to check the name attribute.
driver.findElement(By.xpath("//*[contains(#name,'link-active')]")).click();
xpath has a name attribute method, you can try this
driver.findElement(By.xpath("//*[contains(name(),'link-active')]")).click();
You can simply introduce webdriverwait to let your script knows that it is visible and enabled and then you can try to click. Like this :
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#name,'link-active')]"))).click();
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']
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").
I am unable to locate this element with the class name. Below is the HTML code:
<a class="j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear" title="Stream options" href="#"></a>
I tried to create an xpath using class and title both of them did the work in eclipse...ex:
//a[#title='Stream options']
//a[contains(#class,'j-js-stream-options j-homenav-options jive-icon-med jive-icon-gear')]
..
the None of the above options worked and I tried a few others too...Essentially I want to click on this element and do some action..I want to locate the randomly created xpath so that I can click on the element in the next run.
FYI: the element is a hidden element I need to click on some other element before this element appears. This is a dynamically created element whose expath changes all the time.
Any suggestions would be greatly appreciated...Thanks
Is the element you want to select in a separate iframe? If so, you need to switch to the correct iframe (driver.switchTo().frame("frame-id")) before firing the xpath selector.
Additionally, something to watch out for is that old versions of IE didn't have a native xpath library. See this answer for more details.
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.