0

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?

1
  • I think I've resolved the issue by setting the select to visible, please see my updated answer. Commented Sep 29, 2015 at 23:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried this and while this avoids the error, this does not actually assign the vehicle make. In order to have access to the vehicle year tab, a vehicle make must be selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.