ElBambino_PY
ElBambino_PY

Reputation: 15

Selenium can't select value in dropdown in headless mode

I am learning Python and now I have a problem with Selenium. The program works well when I am logging to the website without the headless mode but fails when I use it. I have to use the headless mode because the code fails on Heroku if I don't disable it.

I would like to select value="year" from:

<select name="duration" id="duration" aria-required="true" class="c-form__select is-touched">
    <option role="option" value="session" aria-selected="false" selected="">This session only</option>
    <option role="option" value="day" aria-selected="false">1 day</option>
    <option role="option" value="week" aria-selected="false">1 week</option>
    <option role="option" value="month" aria-selected="false">1 month</option>
    <option role="option" value="year" aria-selected="false">1 year</option>
</select>

This is my actual code to select the year:

xpath = "//*[@id='duration']/option[text()='1 year']"
duration_field = browser.find_element_by_xpath(xpath)
duration_field.click()

It fails when it clicks the value. Could you help me to solve this problem ? So the program is fully operationnal and ready to be pushed to Heroku.

Upvotes: 1

Views: 611

Answers (1)

NicolasM3
NicolasM3

Reputation: 41

When using headless browser mode, it is recommended to set the size of event. You can do this with this code.

options = webdriver.ChromeOptions()
options.add_argument('--headless')

options.add_argument('window-size=1920x1080'); # add this line to set the size

Upvotes: 1

Related Questions