I tried to click on an element using CSS selector and later Xpath. But failed both. Can anyone help me in solving the issue. Below is the xpath I provided.
Xpath: //*[#id="content"]/div/div[1]/ul/li[3]/div[2]/div/button
Html: Select or search a country in the list... Bahrain
I am new to Selenium and don't have prior experience in automating applications. Should I stick on using xpath or should try using some other locators?
You can either use JavascriptExecutor:
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Or
Get the xpath of the WebElement. Put them in the List (Java Collection)
List<WebElement> lst = xpath ;
for(WebElement we:lst){
if(we.getText().equalsIgnoreCase("Bahrain"))
we.click();
}
}
You can use the code below to search for the desired text and click the element.
String searchText = "Bahrain"; // you can parameterize it
WebElement dropdown = driver.findElement(By.id("content"));
dropdown.click(); // assuming you have to click the "dropdown" to open it
List<WebElement> options = dropdown.findElements(By.tagName("li"));
for (WebElement option : options)
{
if (option.getText().equals(searchText))
{
option.click(); // click the desired option
break;
}
}
Related
I want to select value by text from a dynamic non select dropdown. I did some research and I found this code:
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("/html/body/div[1]/div[2]/div/div/div"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
Basically it put the dropdowns options into a List element, and run through in a for loop, and if the options contains text, click it and break the loop. However it is not working with this type of dropdown:
Snapshot:
Can you suggest what can I do?
Snapshot of Specific value:
Note: The page is only available via private vpn, so I cannot share it.
To select the value by text Teszt_5 from a dynamic non Select dropdown you can use the following locator strategies:
String text = "Teszt_5";
WebElement element = driver.findElement(ByMapper.getBy(dropdown));
element.click();
List<WebElement> options = element.findElements(By.xpath("//div[#class='mat-autocomplete-panel mat-autocomplete-visible ng-star-inserted' and starts-with(#aria-labelledby, 'mat-form-field-label')]//mat-option//span[#class='mat-option-text']"));
for (WebElement option : options){
if (option.getText().contains(text)){
option.click();
break;
}
}
References
You can find a couple of relevant detailed discussions in:
Automating jQuery based bootstrap dropdown using Selenium and Java
How to select a DropDown from a list but HTML don't have select Tag using Selenium Java
I am trying to select a link based on the "href" attribute with an TextLink. I cant use the xpath here because the links keep moving around the page every time the page loads. Kindly advise
My HTML :
<li class="navw2">
购彩大厅
</li>
My CODE :
element=driver.findElement(By.LinkText("购彩大厅")).onlick();
Using x-path, try the following code :
WebElement element = driver.findElement(By.XPath("//li[#class='navw2']/a"));
element.click();
Encoding is the issue here. Not the Xpath. Instead of using LinkText you can go ahead with any of the below options:-
WebElement element = driver.findElement(By.XPath("//li[#class='navw2']/a"));
element.click();
Or
WebElement element = driver.findElement(By.XPath("//a[#href='/index/lobby.html']"));
element.click();
Or
driver.findElement(By.cssSelector("li > a"));
element.click();
Try this xpath:
WebElement element = driver.findElement(By.XPath("//li[#class='navw2']/a[contains(text(),'购彩大厅')]"));
element.click();
If there is possibility of having multiple links of the same type, you can use this:
List<WebElement> elements = driver.findElements(By.XPath("//li[#class='navw2']/a[contains(text(),'购彩大厅')]"));
elements.get(0).click();
Or
List<WebElement> elements = driver.findElements(By.XPath("//li[#class='navw2']/a[#href='/index/lobby.html' and contains(text(),'购彩大厅')]"));
elements.get(0).click();
Code behind button:
<input class=”btn” id=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ name=”mypage:formid:relatedScenaiosListId:j_id27:j_id28″ onclick=”window.open(‘/apex/newscenario?Opportunity__c=006f00000072n8hAAA’,’_top’, 1);;A4J.AJAX.Submit(‘mypage:formid’,event,{‘similarityGroupingId’:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′,’parameters’:{‘mypage:formid:relatedScenaiosListId:j_id27:j_id28′:’mypage:formid:relatedScenaiosListId:j_id27:j_id28′} } );return false;” value=”New” type=”button”>
I did a right click from the Inspect Element view and saw I could copy the Xpath and found it was :
//*[#id="mypage:formid:relatedScenaiosListId:j_id27:j_id28"]
Note the *.
I tired:
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28']“));
txtnew.click();
and
WebElement txtnew = driver.findElement(By.xpath(“//input[#id='mypage:formid:relatedScenaiosListId:j_id27:j_id28'][#value='New']“));
txtnew.click();
but neither worked.
I’m curious about the *, if that should be part of my Xpath statement?
If it's not necessary for you to use xpath, use searching by id, or cssSelectors to find your element. E.g.
//if you have only one element with class btn you can use this selector
//if element placed in parent (and this can identify element much more) add selector to
//parent before .btn
WebElement txtnew = driver.findElement(By.Css(".btn"));
//or
WebElement txtnew = driver.findElement(By.Css("input[value='New']"));
//or if id not generated automatically
WebElement txtnew = driver.findElement(By.Css("#mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
//or using By.Id
WebElement txtnew = driver.findElement(By.Id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
Any of them will work in specific situation. Choose one which more suitable for your sitution. Thanks.
Try this xpath:
input[#value='New'][#class='btn'][starts-with(#id, 'mypage:formid')]
The real issue, that I didn't realize, was that the control was in a different frame. Once I set the driver to look at the frame any number of xpath expressions worked.
` driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.switchTo().frame("066i0000004bNpx");
WebElement txtNewbtn = driver.findElement(By.id("mypage:formid:relatedScenaiosListId:j_id27:j_id28"));
txtNewbtn.click();`
My code is below, I am having problems with 'Production Plan', I need to be able to click the Production Plan link but it doesn't work.
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]"));
ArrayList<String> links = new ArrayList<String>();
for(WebElement we : ddOpts) {
//System.out.println(we.getText());
links.add(we.getText());
System.out.println(links);
if(we.getText().contains("Production Plan")) {
we.sendKeys("Production Plan");
we.click();
}
Your WebElements in ddOpts list aren't an anchor tags but divs. I don't know how does the page look, but it seems you might thought another xpath. Something like:
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div/a[4]"));
or
List<WebElement> ddOpts = driver.findElements(By.xpath("html/body/div[4]/a"));
Or maybe if it is a select option, use a Select object
Select mySelect = new Select(driver.findElements(By.xpath("html/body/div[4]")));
mySelect.selectByVisibleText("Production Plan");
see answer of this question:
How to select an option from a drop-down using Selenium WebDriver with Java?
I didn't understand why your trying to sendKeys().
But If you are trying to click on a link, following will work :
WebElement link = driver.findElement(By.PartialLinkText("Production Plan"));
link.click();
or
You can also try with Explicit wait :
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.PartialLinkText("Production Plan"))).click();
I can't select HTML Dropdown list with my Webdriver method. What was wrong in my code.? Could you give me some hints.
<select>
<option value="32">32</option>
<option value="34">34</option>
<option value="36">36</option>
</select>
public static List<WebElement> chooseSize(Integer size){
WebElement select = findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
List<WebElement> options = select.findElements(By.tagName("option"));
for(WebElement option : options){
if(option.getText().equals(size)){
option.isSelected(); // or .click()?
}
}
return options;
}
There's a support class that can help you with that in WebDriver: "org.openqa.selenium.support.ui.Select".
Here is how you use it:
// First, get the WebElement for the select tag
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
// Then instantiate the Select class with that WebElement
Select select = new Select(selectElement);
// Get a list of the options
List<WebElement> options = select.getOptions();
// For each option in the list, verify if it's the one you want and then click it
for (WebElement we : options) {
if (we.getText().equals(valueToSelect)) {
we.click();
break;
}
}
Select select = new Select(driver.findElement(By.xpath("Xpath_of_Select_Element")));
select.selectByVisibleText("Option_to_Select");
This is the simplest way to select an option from a select drop down
For such cases, I'm using xpath expressions. You'll save a lot of code!
For what you are asking for, this should do (I assume that your xpath is properly targeting the corresponding select):
// Click select first:
// (See http://code.google.com/p/selenium/issues/detail?id=2112)
findElement(By.xpath(DropDown_Article_Size_XPATH_ID)).click();
// Then click option:
String xpathOption = String.format("%s/option[text()='%d']",
DropDown_Article_Size_ID, size);
log.debug("Selecting option by text '{}' using xpath '{}'", size, xpathOption);
findElement(By.xpath(xpathOption)).click();
By the way, I don't get why your chooseSize returns the list of all options. You should probably rename the method to something meaningful (getOptionsBySize, for example, if this is what you want).
Bit modification it works for me, thanks a lot such a simple code it does the job.
Select select = new Select(driver.findElement(By.name("Status_operator")));
select.selectByValue("=");
Have you tried setSelected()? isSelected() is a getter so it won't change anything.
If you are using Selenium2 you have to use option.click().
I'm afraid that there's an issue with ChromeDriver and Select. Tested on Chrome for MacOSX, .click() and .isSelected() don't work. The same code in FireFox, works as expected. Is there anything different between both browsers?
List<WebElement> opciones = select.getOptions();
for(WebElement el : opciones){
System.out.println("Elemento disponible: ["+el.getAttribute("value")+"]["+el.getText()+"]");
//Select actual option
el.click();
if(el.isSelected())
System.out.println("Selected: ["+el.getAttribute("value")+"]["+el.getText()+"]");
}
you can do
WebElement selectElement = driver.findElement(By.xpath(DropDown_Article_Size_XPATH_ID));
selectElement.sendKeys("34")
to select 34
its that simple. Sendkeys is a very useful method in webdriver and has different implementations for different kind of objects i.e. for a textbox Sendkeys would type in the text, while for a select element it would select element.
I have even read that for a file upload field you can do sendkeys to enter the file path.
cheers
Shrikant