Selenium 在无头模式下无法在下拉列表中 select 值

Selenium can't select value in dropdown in headless mode

我正在学习 Python,但现在我遇到了 Selenium 方面的问题。当我在没有无头模式的情况下登录网站时,该程序运行良好,但在我使用它时失败。我必须使用无头模式,因为如果我不禁用它,代码在 Heroku 上会失败。

我想 select value="year" 来自:

<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>

这是我到 select 年份的实际代码:

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

点击数值失败。你能帮我解决这个问题吗?因此该程序已完全运行并准备好推送到 Heroku。

使用无头浏览器模式时,建议设置事件大小。您可以使用此代码执行此操作。

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

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