单选按钮显示未选中但不能单击python selenium

时间:2017-02-22 08:15:31

标签: python selenium click runtime-error

我有一个下面的表格,我想点击肖像模式,但我收到错误,然后我发现我可以在未选择的状态下显示它,以便我认为它是可见的,为什么我有错误?

If

HTML:

radio = browser.find_element_by_id("orientation_landscape_right")
print(radio.is_selected())
radio = browser.find_element_by_id("orientation_portrait")
print(radio.is_selected())
radio.click()
browser.find_element_by_name("commit").submit()

输出和错误:

<form class="img-orientation-form" action="/account/orientation" method="post">
    <input type="radio" name="orientation" id="orientation_landscape_right" value="landscape_right" class="img-orientation-radio landscape-right" checked="checked"><span>(default)</span>
    <input type="radio" name="orientation" id="orientation_upside_down" value="upside_down" class="img-orientation-radio upside-down">
    <input type="radio" name="orientation" id="orientation_landscape_left" value="landscape_left" class="img-orientation-radio landscape-left">
    <input type="radio" name="orientation" id="orientation_portrait" value="portrait" class="img-orientation-radio portrait">
    <input type="submit" name="commit" value="submit">
</form>

1 个答案:

答案 0 :(得分:0)

这是时间问题:目标单选按钮最初位于DOM,但它是不可见的,您在实际变为可见之前尝试单击它。因此,您需要等待一段时间才能看到元素:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

browser.find_element_by_xpath('//a[normalize-space(.)="Settings"]').click() # To expand drop-down menu
wait(browser, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "orientation-button"))).click() # Click on "Image orientation" link
wait(browser, 10).until(EC.visibility_of_element_located((By.ID, "orientation_portrait"))).click() # wait for radio-button visibility + click