【问题标题】:how to click on a href link using Selenium python如何使用 Selenium python 单击 href 链接
【发布时间】:2021-12-03 20:04:21
【问题描述】:

我正在使用 Selenium 和 Python 来尝试点击网页上的按钮,如下所示:

<div id="home" class="sc-gKAaRy bGevZe"><h1 tabindex="-1" id="home_children_heading" class="sc-fujyAs fxSjMq">Vérification</h1>
<p id="home_children_body" class="sc-pNWdM hbrIBL">Veuillez résoudre l’énigme pour que nous sachions que vous n’êtes pas un robot.</p>
<a href="#" aria-describedby="descriptionVerify" id="home_children_button" class="sc-bdnxRM gonizE sc-kEqXSa duYBxC">Suivant</a>
<div id="descriptionVerify" class="Home__StyledHiddenAnnouncement-sc-lzg094-0 kfyjkn">
Défi visuel.Les utilisateurs de lecteurs d'écran doivent utiliser le défi audio ci-dessous.</div></div> 

我尝试了许多解决方案,例如单击 Xpath 和 href,但没有一个是有效的,你能帮帮我吗? 这是我尝试使用它的代码:

xPath = '//*[@id="home_children_button"]'
driver.find_element_by_xpath(xPath).click()

#With other Xpath

xPath = 'xPath = "//input[contains(@type,'submit')][contains(@id,'home_children_button')][contains(.,'Suivant')]"'
driver.find_element_by_xpath(xPath).click()

我需要你的帮助,拜托!

【问题讨论】:

  • 它是否会抛出任何错误,说明未找到该元素?如果是,那么您的 xpath 可能是错误的。

标签: python selenium button click


【解决方案1】:

如果这个 id home_children_buttonHTMLDOM 中是唯一的,那么请尝试使用下面的 xpath :

//a[@id='home_children_button']

代码试用 1:

time.sleep(5)
driver.find_element_by_xpath("//a[@id='home_children_button']").click()

代码试用 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='home_children_button']"))).click()

代码试用 3:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='home_children_button']")
driver.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='home_children_button']")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PS:如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654336@匹配节点。

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 2016-11-06
    • 2022-08-11
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    相关资源
    最近更新 更多