我正在尝试使用 selenium 和 python 单击按钮,但我正在努力寻找正确的语法。 CSS代码如下:
<button aria-label="Weitere Jobs anzeigen" class="infinite-scroller__show-more-button" data-tracking-control-name="infinite-scroller_show-more">
Weitere Jobs anzeigen
</button>
我尝试过的语法是
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[aria-label='Weitere Jobs anzeigen']"))).click()
和
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"[aria-label='Weitere Jobs anzeigen']"))).click()
但都不起作用。如有任何帮助,我们将不胜感激!
修改了提供的 HTML,仅用于帮助指示按钮何时被单击。 否则,问题应该被模拟为相同,请告诉我这是否有帮助。
测试环境:
解决方案:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".infinite-scroller__show-more-button"))).click()
Python:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button_css_selector_path = ".infinite-scroller__show-more-button"
driver = webdriver.Firefox()
driver.get("<URL_OR_HTML_FILE>")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, button_css_selector_path))).click()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<button aria-label="Weitere Jobs anzeigen" class="infinite-scroller__show-more-button" data-tracking-control-name="infinite-scroller_show-more" onclick="alertUser()">
Weitere Jobs anzeigen
</button>
<script>
const alertUser = () => alert("Button Has Been Clicked!")
</script>
</body>
</html>
GIF 太大了,显示了我所做的一切,请查看这里