【问题标题】:How to select a radio button where its tag is hidden如何选择隐藏其标签的单选按钮
【发布时间】:2016-02-07 09:16:54
【问题描述】:

我正在学习 python 并尝试在单选按钮上进行基本点击。目前选择了“返回”单选按钮,而我希望选择“单向”单选按钮。但是,当我运行我的代码时,它指出它找不到“单向”单选按钮。

我想问我需要做什么才能找到收音机,但它隐藏在各种其他标签下?

下面是基本代码:

element = driver.find_element_by_xpath("//*[contains(@class, 'option')]")
one_way = element.find_element_by_id('one-way').click()

更新的代码(仍然无法正常工作):

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    driver = webdriver.Firefox()
    driver.maximize_window()
    driver.get("https://xxxxx/xxx/")

    wait = WebDriverWait(driver, 10)
    actions = ActionChains(driver)

    # wait for the page to load
    wait.until(EC.presence_of_element_located((By.ID, "search-form-content")))

    # fill out the form

    element = driver.find_element_by_id('one-way')
    driver.execute_script('return arguments[0].scrollIntoView();', element)
    element.click()

   # previously just tried driver.find_element_by_id('one-way').click() and    #one_way = driver.find_element_by_xpath("//div[@class_name='carat']/div[@class_name='option']/input[@id='one-way']").click()

【问题讨论】:

    标签: python python-2.7 selenium


    【解决方案1】:

    Selenium 默认不支持与不可见/隐藏元素交互。您可以获取选项的所有文本/值,但不能与它们交互。

    隐藏此元素或选项有一个的原因:用户可能不会选择它。例如,一个选项仅适用于高级用户。在这种情况下,进行验证检查的良好网络服务器将引发错误。

    另外一句话:

    一般来说,您可以使用 Select-Element 的概念,就像 Selenium 官方文档中解释的那样(对于可见元素):

    select = driver.find_element_by_tag_name("select")
    allOptions = select.find_elements_by_tag_name("option")
    for option in allOptions:
        print "Value is: " + option.get_attribute("value")
        option.click()
    

    http://www.seleniumhq.org/docs/03_webdriver.jsp

    【讨论】:

    • 由于您提到了高级用户,这听起来像是一个非常合法的测试用例,以确保您在服务器端验证这一点,即使客户端篡改了隐藏选项。
    【解决方案2】:

    由于元素id 是唯一的,因此您实际上不需要代码的第一行。使用:

    element = driver.find_element_by_id('one-way')
    

    这将选择所需的元素。但是,如果元素不在视图中(即在浏览器中不可见),您可能需要在单击之前滚动到该元素。为此,请使用:

    driver.execute_script('return arguments[0].scrollIntoView();', element)
    

    这会将元素滚动到视图中。然后你可以点击它:

    element.click()
    

    【讨论】:

    • 这是我第一次尝试的,但它表示这是一个错误:'ElementNotVisibleException:消息:元素当前不可见,因此可能无法与之交互'
    • 这似乎也不起作用。我会给你网络的网址,这样你就可以看到我在做什么。 reservations.jet2.com/mobile。当页面打开时,只需单击“单向”按钮。我把我当前的全部代码(包括你的例子)放在问题中
    • 尝试代码时出现当前错误:WebDriverException: Message: this.getChromeWindowFromDocumentWindow(...) is undefined
    【解决方案3】:

    使用 JAVASCRIPT/jQuery 点击.. 即使您的元素不可见,它也会起作用..

    driver.executeScript("$x('your xpath')[0].click());

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 2017-04-22
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      相关资源
      最近更新 更多