I would like to click checkbox based on link text. There are several checkbox in the page so using the link text I wanted to find value of the checkbox so that I can click on the checkbox
Note # All values are dynamically generated
Can you please help correct the code to include this logic. Thanks
driver.get(new URI(driver.getCurrentUrl()).resolve("/admin/lms/tag").toString());
String tag_name = sheet1.getRow(j).getCell(0).getStringCellValue();
driver.findElement(By.linkText(tag_name)).click();
WaituntilElementpresent.isExist();
String tag_value = sheet1.getRow(j).getCell(1).getStringCellValue();
driver.findElement(By.cssSelector("a[href*='"+tag_value+"']")).click();
WaituntilElementpresent.isExist();
String product = sheet1.getRow(j).getCell(2).getStringCellValue();
WaituntilElementpresent.isExist();
driver.findElement(By.cssSelector("input[name='products[]'][value='11']")).click();
https://i.stack.imgur.com/mOBY2.png
You could use an xpath like this:
//tr[.//*[text()='OPIOIDMORTEPID']]//input
this means to locate the td that has this exact text and find the input from it.
If you want to use partial text then:
//tr[.//*[contains(text(), 'MORTEPID')]]//input
You can do it with the following strategy:
find the link by using the link text
get the parent tr element
from there, find the input element of the checkbox
click on the checkbox
This can be done as follows:
WebElement link = driver.findElement(By.linkText("OPIOIDMORTEPID"));
WebElement trElement = link.findElement(By.xpath("../.."));
WebElement checkboxElement = trElement.findElement(By.tagName("input"));
checkboxElement.click();
Check this out:
Get a list of checkbox WebElements that have a common locator i.e. a common class name or tag name (this WebElement would have to have the text or be a parent of the element that has the text).
List<WebElement> checkBoxElements = webDriver.findElements(By.cssSelector("checkbox"));
Iterate through the list and check if the WebElement has the text you are looking for. If it does, click it!
for (WebElement e : checkBoxElements) {
if (e.getText().contains("Something dynamic")) {
e.click();
}
}
Be happy that this works.
I would write this section as a function so that it's reusable.
public static void selectByProductName(String value)
{
driver.findElement(By.xpath("//tr[//a[text()='" + value + "']]/td/input")).click();
}
and then call it like
selectByProductName("OPIOIDMORTEID");
Related
I want to click on an element which is from a list. I am using getAttribute("value") to get the text, but it returns 0, hence it is not clicking the element. Please help.
DOM structure
<ol class ="class1">
<li value="foo1" class="class2">
<li value="foo2" class="class2">
</ol>
Xpath:
#FindBy(xpath = "//ol[#class='class1']/li")
List<WebElement> tagList;
I have tried getText() but it returns the text along with unknown character, as the element has icons along with the text.
This is my code
public void selectTag() {
addReservationBtn.click();
tags.click();
for(WebElement li : tagList) {
// System.out.println(li.getAttribute("value"));
if (li.getAttribute("value").equalsIgnoreCase("foo2")) {
li.click();
break;
}
}
As text contains unknown characters so
li.getAttribute("value").equalsIgnoreCase("foo2")
will not work as both string not equal.
You can try contains like
li.getAttribute("value").contains("foo2")
I think reason for your problem may be
1. Wrong xpath or 2. Waits
// get second <li> using correct xpath
#FindBy(xpath = "//ol[#class='class1']/li[2]")
List<WebElement> tagList;
public void selectTag() {
addReservationBtn.click();
tags.click();
// waitforliTextToAppearAfterClick();
if (tagList.get(1).getAttribute("value").trim().equalsIgnoreCase("foo2")) {
tagList.get(1).click();
}
You are using #FindBy which only returns the first element found by the given locator. Use #FindBys to get all the elements found by the given locator.
As the previous answer was wrong I've striked it but kept it for
the clarification of the comments.
You can directly get the second element without using the for loop by
#FindBy(xpath = "//ol[#class='class1']/li[#class='class2']")
The getAttribute() method returns null if there are no attribute found and returns empty when there is an attribute but it doesn't contain any value.
I am trying to do automation for one of my application using selenium webdriver. The problem is I am trying to fetch the data from table which contains the value inside a "div" tag.The table is nothing but a calendar.
This is my code for fetching the value from table.(i.e.,div tag)
public boolean webElement_Table_findCellValue_WD(String locatorType, String locatorVal, String cellText){
boolean elementStatus = false;
WebElement tbl = this.getWebElement(locatorType, locatorVal);
List<WebElement> tbTag=tbl.findElements(By.tagName("table"));
int num=tbTag.size();
System.out.println("hhhhhhhhhhhhhhhhhhh"+num);
for(WebElement tbTagEle: tbTag)
{
List<WebElement> trTag=tbTagEle.findElements(By.tagName("tr"));
System.out.println("iiiiiiiiiiiiii"+trTag.size());
for(WebElement trTagEle: trTag)
{
List<WebElement> tdTag=trTagEle.findElements(By.tagName("td"));
System.out.println("jjjjjjjjjjjjj"+tdTag.size());
for(WebElement tdTagEle: tdTag)
{
List<WebElement> divTag=tdTagEle.findElements(By.tagName("div"));
for(WebElement divTagEle: divTag)
System.out.println("the contents are:"+divTagEle.getText());
}
}
}
return elementStatus;
}
My intention is just to select(click) a date from the calender.(Table) which I will pass it through a properties file.
In answer to the first part of the question, #Striter Alfa got most of the way.
The following will print the content of each <div> within each cell within each table (whether one or multiple):
for (WebElement elem : driver.findElements(By.cssSelector("table td div"))) {
System.out.println("Each div:" + elem.getText());
}
I don't know whether you need to store those values in a List. The rest of your question isn't very clear, but hopefully you will be able to solve it from here.
The following will find the specific date table and click on the same.pass date value as parameter.
WebElement el = driver.findElement(By.xpath("//table//div[contains(.,'"+date+"')]"))
el.click();
I am using the sendKeys(key,Keys.TAB) method to navigate through a form.
Actions action = new Actions(driver);
CharSequence key = null;
for(int i=0;i<42;i++)
{
action.sendKeys(key,Keys.TAB).build().perform();
}
At the end of every action(a tab key press) I want to know which form element is selected
I want to reach to the 42nd element of the form and cross check whether its the desired element and for that i need to retrieve some of its information.
I am new to selenium and I am not able to figure out a way to achieve this.
You can use WebDriver's TargetLocator class for this purpose.
WebElement currentElement = driver.switchTo().activeElement();
This will return you with current element it focussed currently. If no element is focussed it will return you the body element, which is the case when u launch your browser.
Internally it will be returning u the element returned by document.activeElement. So to verify u can always run as:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement currentElement = (WebElement) js.executeScript("return document.activeElement");
I am trying to dynamically search an "li" tag item and double-click on this website: www.jstree.com (the right-top hierarchy tree sample). The code does find the WebElement but does not do anything. I am trying as follows. Can someone please point to what I am doing wrong? I am using Firefox 35.0.1 and selenium 2.44.0.
driver.get(baseUrl + "http://www.jstree.com/");
WebElement we = driver.findElement(By.xpath("/html/body/div/div/div[1]/div[1]/div[2]/div[1]/ul/li[1]/ul"));
Actions action = new Actions(driver);
List<WebElement> liItems = we.findElements(By.tagName("li"));
for(WebElement liItem:liItems)
{
System.out.println(liItem.getText());
if(liItem.getText().startsWith("initially open"))
{
System.out.println("Found it...");
liItem.click();
action.moveToElement(liItem).doubleClick().build().perform();
break;
}
}
I ended up doing this:
Modified the selector to make sure ONLY the expected elements are returned. It helps a lot in terms of execution time and reducing the number of unwanted loopings. And, then find the element in run time and use Action() on that to perform double click. I also update the Selenium binding as #alecxe suggested to work with latest Firefox
public void DemoTest() throws InterruptedException {
List<WebElement> liItems = driver.findElements(By.xpath("//*[contains(text(),'initially open')]"));
for(WebElement liItem:liItems)
{
Actions actions = new Actions(driver);
actions.moveToElement(liItem).doubleClick().build().perform();
}
}
We are using Selenium WebDriver and JBehave to run "integration" tests on our web-app. I have a method that will enter a value into a form input.
#When("I enter $elementId value $value")
public void enterElementText(final String elementId, final String value) {
final WebElement webElement = webdriver.findElement(By.id(elementId));
webElement.clear();
webElement.sendKeys(value);
}
But when I try to use this to select an item in a drop-down list it (unsurprisingly) fails
java.lang.UnsupportedOperationException: You may only set the value of
elements that are input elements
How do I select a value in the combo?
This is how to do it:
#When("I select $elementId value $value")
public void selectComboValue(final String elementId, final String value) {
final Select selectBox = new Select(web.findElement(By.id(elementId)));
selectBox.selectByValue(value);
}
The Support package in Selenium contains all you need:
using OpenQA.Selenium.Support.UI;
SelectElement select = new SelectElement(driver.findElement( By.id( elementId ) ));
select.SelectByText("Option3");
select.Submit();
You can import it through NuGet as a separate package: http://nuget.org/packages/Selenium.Support
By using ext js combobox typeAhead to make the values visible in UI.
var theCombo = new Ext.form.ComboBox({
...
id: combo_id,
typeAhead: true,
...
});
driver.findElement(By.id("combo_id-inputEl")).clear();
driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need");
driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.id("combo_id-inputEl")).sendKeys(Keys.ENTER);
If that doesn´t work this is also worth a try
driver.findElement(By.id("combo_id-inputEl")).sendKeys("The Value you need");
driver.findElement(By.className("x-boundlist-item")).click();
The Selenium paradigm is that you are supposed to simulate what a user would do in real life. So that would be either a click or a keys for navigation.
Actions builder = new Actions( driver );
Action action = builder.click( driver.findElement( By.id( elementId ) ) ).build();
action.perform();
As long as you get a working selector to feed into findElement you should have no problem with it. I have found CSS selectors to be a better bet for things involving multiple elements. Do you have a sample page?