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();
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 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;
}
}
I was just playing around automating gmail website using Selenium with Java and I was stuck at the language selection drop down in gmail home page. After selecting the dropdown, I am not able to select any specific language from the listbox as I am getting element not visible exception. How can we select elements from such list boxes? I have already tried using many general techniques like Actions, explicit wait etc.
After login the following code will, as an example, select language Dansk. Important here is to use the Selenium Select class. For documentation see: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement next = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//content/span[contains(text(),'Next')]")));
next.click();
WebElement settings = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'aos T-I-J3 J-J5-Ji')]")));
settings.click();
WebElement settingsmenuchoice = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(#class,'J-N aMS')]")));
settingsmenuchoice.click();
WebElement select = wait.until(ExpectedConditions.elementToBeClickable(By.id(":m4")));
select.click();
Select languageDropdown = new Select(select);
List<WebElement> Options = languageDropdown.getOptions();
for(WebElement option:Options){
if(option.getText().equals("Dansk")) {
option.click();
}
}
I tried to do the same as you, I succeed by changing the language but also I couldn't choose the language I want. here is the method I used:
driver.findElement(By.xpath("//div[#class='u7land']")).click();
driver.findElement(By.xpath("//div[#class='ry3kXd Ulgu9']")).click();
driver.findElement(By.xpath("//div[#class='OA0qNb ncFHed']")).click();
My problem mainly is that my code doesn't run, I tried for more than 2 hours. I have seen many posts also, but some are written in different computer languages (not in Java), so I am confused now.
Below is my code for just clicking a button. All I want to do is click a button and go to new page.
WebDriver driver = new HtmlUnitDriver();
driver.get("file:///C:/Users/Sanya/Desktop/New%20folder%20(2)/page%203%20alerts.htm");
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Try this it works fine for me:
WebElement menuHoverLink = driver.findElement(By.id("your_id"));
actions.moveToElement(menuHoverLink).perform();
You can try the below one...
Actions action = new Actions(driver);
action.click(driver.findElement(By.partialLinkText("Alert"))).build().perform();
It was worked for me :-)
You can use XPath for instance to locate the element on your page:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
Here is more information about how XPath works.
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