提问人:B.12 提问时间:7/29/2023 最后编辑:undetected SeleniumB.12 更新时间:8/3/2023 访问量:86
如何使用 Python 机器人单击按钮元素
How to click on a button element using a Python bot
问:
我想单击下面代码中的发送电子邮件按钮,但我无法访问任何字段,例如选择器,xpath...,然后单击按钮。当您运行代码时,您将看到它已经打开和关闭了页面。我怎样才能在这里提供一个流程?
代码试用:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
# options.add_argument("--headless")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.delete_all_cookies()
driver.get(
"https://suchen.mobile.de/fahrzeuge/details.html?id=355902358&damageUnrepaired=NO_DAMAGE_UNREPAIRED&isSearchRequest=true&pageNumber=1&scopeId=C&sortOption.sortBy=relevance&action=topOfPage&top=1:1&searchId=034dde38-e567-1e56-6d64-b8c8783a0e20&ref=srp")
time.sleep(10)
'''
email_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'E-mail schreiben')))
email_button.click()
'''
try:
button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'E-Mail schreiben')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'E-Mail schreiben')]"))).click()
except Exception as e:
print("Error:", e)
time.sleep(5)
driver.quit()
我尝试使用:
完整的 Xpath:
/html/body/div[5]/div/div[2]/div[3]/div[2]/aside/div[1]/div/div[4]/div[2]/div/div/span
选择器:
#email-link-top > span
Xpath:
//*[@id="email-link-top"]/span
元素 HTML:
<span class="btn btn--primary btn--l u-full-width"><i class="gicon-email-white-s icon--s"></i>E-Mail schreiben</span>
答:
0赞
Yaroslavm
7/29/2023
#1
你不能点击按钮,因为你有拦截点击的同意模式。 所以你需要接受模态,然后点击按钮
喜欢
driver.get("https://suchen.mobile.de/fahrzeuge/details.html?id=355902358&damageUnrepaired=NO_DAMAGE_UNREPAIRED&isSearchRequest=true&pageNumber=1&scopeId=C&sortOption.sortBy=relevance&action=topOfPage&top=1:1&searchId=034dde38-e567-1e56-6d64-b8c8783a0e20&ref=srp")
wdwait = WebDriverWait(driver, 10)
consent = wdwait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#mde-consent-modal-container [class*=mde-consent-accept-btn]")))
consent.click()
wdwait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "#mde-consent-modal-container")))
wdwait.until(EC.element_to_be_clickable((By.ID, "email-link-top"))).click()
0赞
undetected Selenium
7/30/2023
#2
要单击元素而不是 presence_of_element_located(),您需要诱导 WebDriverWait 的 element_to_be_clickable(),并且可以使用以下任一定位器策略:E-Mail schreiben
代码块:
driver.get("https://suchen.mobile.de/fahrzeuge/details.html?id=355902358&damageUnrepaired=NO_DAMAGE_UNREPAIRED&isSearchRequest=true&pageNumber=1&scopeId=C&sortOption.sortBy=relevance&action=topOfPage&top=1:1&searchId=034dde38-e567-1e56-6d64-b8c8783a0e20&ref=srp") # accept the cookies WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.mde-consent-accept-btn"))).click() # click on E-Mail schreiben WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='email-link-top-container']//span[contains(text(), 'E-Mail schreiben')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
评论
1赞
B.12
8/2/2023
是的!非常感谢。
0赞
B.12
8/3/2023
我能找出你用哪个扩展程序找到这个xpath吗?因为我试图选择相同数量的其他字段,但它给出了 xpath 错误。我使用了Chropath,XpathFinder等应用程序,但没有给出任何结果。
0赞
undetected Selenium
8/3/2023
@B.12 对不起,没有 Chropath 或 XpathFinder,我们自己编写 xpath/cssSelector 观察 DOM,最终每个自动化迟早都应该能够做到。如果需要,请寻求 FireBug 的帮助。
1赞
B.12
8/3/2023
好的,非常感谢。
评论