【问题标题】:Python Selenium Clicking a javascript Radio ButtonPython Selenium 单击 javascript 单选按钮
【发布时间】:2019-03-17 07:41:28
【问题描述】:

我在我的脚本的末尾,这一定是我必须通过的最后一页,我被卡住了。我花了 3 个小时尝试不同的组合和不同的方法来加载元素。

页面是大量的 javascript,所以当我尝试获取页面源时,它给了我

This page uses frames, but your browser doesn't support them.

我发现窗口内有两个框架,并尝试对两个框架应用所有尝试。

我尝试选择框架时的结果

**driver.find_element_by_xpath('''//*[@id="three"]/tbody/tr[2]/td/div[2]/a/input''').click()**

error

**Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="three"]/tbody/tr[2]/td/div[2]/a/input"}**

我也尝试过以下在所有框架上使用或不使用[0] 的方法仍然是同样的问题

**driver.find_element_by_xpath('.//input[@type="radio" and @value="05"]')[0].click**

我试过了,但也遇到了同样的错误

**element = driver.find_element_by_id("reason")**

下面是检查器窗口中代码的屏幕截图。 screenshot of inspector

【问题讨论】:

  • 请在问题中显示代码的相关部分,而不是屏幕截图。有关更多信息,请参阅here

标签: python selenium web screen-scraping


【解决方案1】:

先尝试切换到包含框架:

parent_frame=driver.find_element_by_css_selector('your selector')
driver.switch_to.frame(parent_frame)

#select the button after

How to identify and switch to the frame in selenium webdriver when frame does not have id

【讨论】:

    【解决方案2】:

    尝试让驱动程序单击单选按钮

    element = driver.find_element_by_xpath("//table[@id="three"]//input[@name="reason"]")
    element.click();
    

    或javascript执行器

    element = driver.find_element_by_xpath("//table[@id="three"]//input[@name="reason"]")
    driver.execute_script("arguments[0].click();", element)
    

    【讨论】:

      猜你喜欢
      • 2014-09-21
      • 2015-10-18
      • 2017-09-12
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2021-09-03
      • 2018-04-30
      相关资源
      最近更新 更多