How can I click on different a elements with different id attribute values in Selenium Framework?
Link ahref value are the same text.
<li>
<a id="abc_differentName_1_default_fun" href="javascript:startClient('abc_differentName_1', 'default', 'fun');">Start</a>
</li>
<li>
<a id="abc_differentName_2_default_fun" href="javascript:startClient('abc_differentName_2', 'default', 'fun');">Start</a>
</li>
<li>
<a id="xyz_differentName_xyz_default_fun" href="javascript:startClient('xyz_differentName_xyz', 'default', 'fun');">Start</a>
</li>
I don't want to create every link one click method.
My problems are I don't want to create every link one click method. I want one click method depends on the id name. Possible to create a click method example: void clickByIdName(String idName) ??
Depends on which link i clicked, it will open new windows(other page object).
Many thanks in Advance.
If there are multiple links with similar text on page, you can use id value to select each element as follows:
driver.findElement(By.cssSelector("a#abc_differentName_1_default_fun")).click();
driver.findElement(By.cssSelector("a#abc_differentName_2_default_fun")).click();
driver.findElement(By.cssSelector("a#xyz_differentName_xyz_default_fun")).click();
You don't have to use these ID attribute values to locate the link elements. There is a better way - a link text locator. You can locate all links with Start link text and choose which one you need getting it by index:
List<WebElement> links = driver.findElements(By.linkText("Start"));
links.get(0).click();
Not sure what's the exact problem here, but what is wrong with these:
driver.findElement(By.id("abc_differentName_1_default_fun")).click();
driver.findElement(By.id("abc_differentName_2_default_fun")).click();
driver.findElement(By.id("xyz_differentName_xyz_default_fun")).click();
You can use css selector to find all elements with id contain something like:
driver.findElements(By.cssSelector("[id*=_differentName_]"))
If you want void clickByIdName(String idName)
void clickByIdName(String idName) {
driver.findElement(By.Id(idName)).click();
}
Related
I am trying to access a web element in this example I am trying to access the className
driver.findElement(By.className("more-option")).click();
from bellow but it fails.
<div class="text-center">
<a class="small text-muted js-more-options" href="#">More
Options</a> = $0
</div>
My goal is to be able to test the ability to click More options button
Edit
I have tried
driver.findElement(By.cssSelector("td[title='More options']")).click();
and
driver.findElement(By.partialLinkText("options")).click();
There are to many option you can click on element.You can use contains().
Contains() is a method used in XPath expression.
driver.findElement(By.XPath("//a[contains(text(),'More Options')]")).Click();
or
driver.findElement(By.XPath("//a[contains(#class,'small')]")).Click();
if you get more than one element then you have to use index and click on particular element.
Find element using By.className just for single class name.
Try following approach.
By css selector:
driver.find_element_by_css_selector('div.text-center a.small.text-muted.js-more-options').click()
driver.find_element_by_css_selector('a[class="small text-muted js-more-options"]').click()
By xpath:
driver.find_element_by_xpath('//div[#class="text-center"]//a[#class="small text-muted js-more-options"]').click()
By partial link text:
driver.find_element_by_partial_link_text('Options').click()
I'd like to select some schemas on the screen for testing. But there are 8 different types schemas. And I couldn't select any schema without click on the schema's image. The element is displayed like this;
I created web element list and filtered it on value that i wanted to select schema name with java 8 stream than i can replace filtered element with (up div[class=' up template-list-board row']>div>P to div[class='template-list-board row']>div>a) , but it was not a good idea, i've experienced.
So , is there any way to click that "a" tag?
The html is displayed like this;
<div class="template-list-board row">
<div class="template-item-list">
<a class="item-image-click" ....> </a>
<p class="item-name">Meeting Agenda</p>
</div>
<div class="template-item-list">
...
</div>
I am not sure if I am understanding you correctly, but my solution would be something like this (with XPATH not css):
1) create a string for the locator:
String locator = "//div[class='template-list-board row']//p[text()='" + hereYourNameVariableAsString + "']/preceding-sibling::a";
2) Then create your element :
WebElement element = driver.findELement(by.xpath(locator));
Here the important thing is that you locator is dynamic you will have to provide you menu name or whatever that name represent such as "Meeting Agenda". Then it will find that menu and the previous sbling a for clicking. I hope I understand the problem correctly and this helps.
Basically there are 3 types of Save button in a page. Now, the button which I'm trying to click on is a type="button" and remaining types of save are not defined as type="button".For all three save buttons LinkText is defined as Save. So, is there any way to click on type="button" by linkText.
HTML:
<button class="btn btn-md bgm-blue m-r-10 waves-effect" ng-click="updateUser()" type="button">Save</button>
Code which I tried:
List<WebElement> list = Util.getWebDriver().findElements(By.xpath("//*[text()='Save']"));
System.out.println("SaveButton"+list.size()); ///Returning 3 save button in a page
list.get(3).click();
Now, suppose in one page there are 4 save buttons and in another page there are 3 save buttons. So, it is not possible to make a method because each time index will differ.
If there is any way to find xpath by type="button". Will be easy for me to make a method and call it each time I want to click on"Save".
Please let me know in case of clarification.
is there any way to click on type="button" by linkText
Actually By.linkText() is use to locates <a> elements only that contain the given link text while you're trying to locate <button> element. So you can not locate desire element using By.linkText().
button which I'm trying to click on is a type="button" and remaining types of save are not defined as type="button"
As you are saying only desire button contains attribute type="button", So it is very easy to find that element using other locator as below :-
By.cssSelector() :
button[type='button']
button[type='button'][ng-click='updateUser()']
button.btn.btn-md.bgm-blue.waves-effect[type='button'][ng-click='updateUser()']
By.xpath() :
//button[text()='Save' and #type='button']
//button[.='Save' and #type='button']
//button[text()='Save' and #type='button' and #ng-click='updateUser()']
//button[.='Save' and #type='button' and #ng-click='updateUser()']
As the Save button contains class attribute you can construct an xpath as follows:
findElements(By.xpath("//button[#class='btn btn-md bgm-blue m-r-10 waves-effect'][contains(text(),'Save')]"));
I'm trying to locate and click an element on my page but can't use the by.id method as the id's are generated and change per session. For most elements I can get around this by using xpath but there is a dropdown menu where this does not work. I can click the element containing the dropdown and it shows me the options. If I locate the element I need and copy it's xpath the test case won't function stating it can't find the xpath. Now next to the id the Element I'm trying to click also has a class. Problem is that this class is not unique, all menu items in the dropdown have the same class with a different text. What I would like to do is something like:
driver.findeElement(By.class("x-menu-item-text").equals("Unique text 1here").click()
The class "x-menu-item-text" is not unique but the text in this particular class is. I can't use the ID as this is automatically generated. The full code for the item or element I want to click is:
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 1 here</span></a>
<a id="ext-comp-1035" class="x-menu-item" hidefocus="true" unselectable="on" href="#"><span id="ext-gen250" class="x-menu-item-text">Unique text 2 here</span></a>
I'm using Selenium Webdriver with Eclipse (Java).
Allthough the answer provided seems to work on most pages and locations, there is a situation however where I can't get it to work. Can anyone advise?
There is a page with buttons and I want to click one of these buttons. If I use the following statement:
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Add']")).click();
I get an error message
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
If I look at the source I see:
<button class=" x-btn-text" id="ext-gen539" type="button">Add</button>
So the element is present and visible.
I've tried adding a wait.until statement before the click statement but this does not work either:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")));
driver.findElement(By.xpath("//*[#class=' x-btn-text' and text()='Toevoegen']")).click();
Extra information: could this problem be because I'm looking for an element that is located in a popup?
You can use xpath locator
https://newcircle.com/bookshelf/selenium_tutorial/locators
By.xpath("//span[#class='x-menu-item-text' and text()='Unique text 1here']")
How can I simulate a click on the respective tag <a> below and grab the link contained in it?
<span class="textAlignment nextPage">
<a class="jsEnabled nextBtn cursorPointer" href="javascript:setSelectedLink('NextPageButton');" title="Next page" alt="Next page"></a>
</span>
Using htmlunit as follows to get the element but I only get null
HtmlAnchor a = page.getFirstByXPath("//a[#class='jsEnabled nextBtn cursorPointer']");
In order to simulate a click you should first fetch the element from the HtmlPage. I would use getFirstByXPath.
Then just perform the click method on the returned object.
It is unclear what you mean by first clicking in a link and then grabbing the link contained in it. If you want to, apart from clicking the anchor, getting the content of the href attribute before clicking on it then you should fetch the anchor using the same method mentioned above and before clicking on it perform the getHrefAttribute.