簡體   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