Reputation: 19
I am trying to fill out the dropdown menus found on this homepage using Python and the selenium package. To Select Make I am using the following code
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
driver.implicitly_wait(5)
driver.get('http://www.tirerack.com/content/tirerack/desktop/en/homepage.html')
button = driver.find_element_by_tag_name('button')
ActionChains(driver).click(button).perform()
select_make = driver.find_element_by_id('vehicle-make')
Select(select_make).select_by_value("BMW")
However this does not seem to actually "Select the BMW" option. I tried to follow the method explained in this post. Can someone show me what I am doing wrong?
Upvotes: 0
Views: 2581
Reputation: 2140
From the question you linked to the accepted answer iterates over the options and finds the matching text.
select_make = driver.find_element_by_id('vehical-make')
for option in select_make.find_elements_by_tag_name('option'):
if option.text == 'BMW':
option.click() # select() in earlier versions of webdriver
break
Running this in Java I got the message that the element is not visible, so I forced it:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
Thread.sleep(3000);
driver.findElement(By.tagName("button")).click();
WebElement select_make = driver.findElement(By.id("vehicle-make"));
select_make.click();
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsDisplay = "document.getElementById(\"vehicle-make\").style.display=\"block\"";
js.executeScript(jsDisplay, select_make);
for (WebElement option : select_make.findElements(By.tagName("option"))) {
System.out.println(option.getText());
if ("BMW".equals(option.getText())) {
option.click();
break;
}
}
If you add the JavascriptExecutor lines (in python) I think it will work.
Upvotes: 1