Can't select option with Selenium Webdriver
I have very strange problem. Using selenium I am writing simple web-bot trying to fill page with data, submit them, and harvest results.
I fill all the forms with no problems at all, but than I have to first enter the ZIP code, than click somewhere else for AJAX to list all the possibilities, than select the appropriate option (I want to always select the first one).
But my problem is, I simply can't select it. I fill the ZIP, click the option list itself, wait to "please select" message to get lost (by this time my choice should be there) and than selecting it. I tried option.click()
, I tried selectByVisibleText()
, and even the deprecated setSelected()
. Nothing happens. All I see in FF is drop-downed list of option, with the first one being marked, but that's all. I tried many ways, with no luck at all.
There is my last-attempt code:
ZIPCode = driver.findElement(By.id("formparam_data2_zip")); //get and fill ZIP
ZIPCode.sendKeys(ZIP);
address = driver.findElement(By.name("formparam_data2_zip_id")); // click to fire AJAX
address.click();
(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) { // wait until AJAX shows results
WebElement elm = d.findElement(By.id("formparam_data2_zip_id"));
List<WebElement> options = elm.findElements(By.tagName("option"));
for(WebElement w : options){
if(w.getText() != "Prosím, vyberte."){
return true;
}}
return false;
}});
List<WebElement> options = address.findElements(By.tagName("option"));
options.get(0).click(); // click first option - ! this failes !
phaseTwoBtn = driver.findElement(By.id("formparam_data2_next")); /开发者_运维知识库/ than submit...
phaseTwoBtn.submit();
I had a similar problem and had better results using the Actions class and then making sure to use the moveToElement() method before clicking on it.
Actions builder = new Actions(d);
builder.moveToElement(options.get(0)));
builder.click();
builder.build().perform();
The moveToElement method makes sure the element is in the visible window
Using key board keys we can solve this problem in selenium webdriver .Code for the above example,ZIPCode.sendkeys (ZIP);ZIPCode.sendkeys (Keys.Tab); ZIPCode.sendkeys (Keys.Return);
try this
if(!w.getText().equals("Prosím, vyberte.")){
return true;
}
精彩评论