繁体   English   中英

Selenium无法点击网页python的按钮

[英]Selenium can't click the button of a webpage python

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

按钮的 HTML 代码

尝试这个:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM