【问题标题】:Selenium can't click the button of a webpage pythonSelenium 无法点击网页 python 的按钮
【发布时间】:2020-06-12 03:01:49
【问题描述】:

我正在尝试单击网页上的投票按钮。我可以使用我的 python 代码浏览页面并单击单选按钮。投票按钮未在 HTML 代码中声明为“按钮”或“输入”元素。我真的不确定如何让 Selenium 导航到它并单击它。该按钮本身不链接到网址。我曾尝试使用选项卡然后按回车键,但它会在按钮本身上进行选项卡。我可以提供进一步的 HTML 和任何其他补充信息。下面是 HTML 的图片!

HTML Code of the button

【问题讨论】:

  • 据我所知,HTML 中的“a”是一个“锚元素”。我不懂 HTML,所以我很难找到解决此按钮操作方式的方法。
  • 也许尝试通过 xpath 查找元素,看看是否有效?
  • 我已经尝试过这条路线,但即使使用 xpath 我也无法点击该元素。就好像它没有被识别为可点击项目一样。
  • 如果可能的话,你能添加一个指向网页的链接吗?

标签: python html selenium selenium-webdriver browser-automation


【解决方案1】:

试试这个:

from selenium import webdriver

driver_options = webdriver.ChromeOptions()
chromedriver = "path/to/chromedriver"

driver = webdriver.Chrome(chromedriver, options = driver_options)
driver.get("https://threerivers.okvype.com/2020/02/24/vote-now-okmulgee-area-preseason-baseball-poll-presented-by-muscogee-creek-nation-poll-ends-3-2/")
buttonpath = '//*[@id="pd-vote-button10509753"]'
dotpath = '//*[@id="PDI_answer48619888"]'
dot = driver.find_element_by_xpath(dotpath)
vote = driver.find_element_by_xpath(buttonpath)
dot.click()
vote.click()

通过选择要单击的正确元素,它对我有用。有时按钮周围有一个包装元素,它必须接收点击而不是目标元素,才能触发响应。

【讨论】:

    【解决方案2】:

    你能使用类选择器找到元素吗?

    我会按类获取 SelenideElement,然后调用 click 方法:$('.pds-vote-button')

    【讨论】:

    • 我已经尝试过使用类选择器的方法。它没有用。我的实现可能不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2021-07-10
    • 1970-01-01
    • 2015-03-08
    相关资源
    最近更新 更多