使用 Selenium 单击 Python 中的按钮
Click button in Python with Selenium
按钮在 HTML 中看起来像这样:
<button type="submit" class="btn btn-success"> <strong>Gönder</strong></button>
我在 python 中使用了这个:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
b = webdriver.Firefox()
b.get("####")
url = b.find_element_by_name("link")
url.send_keys("####")
b.find_element_by_xpath("//button[@type='submit']").click()
还有更多按钮 type="submit"
我只需要调用 1 个特定按钮
有谁知道我怎样才能点击按钮。
如果可能的话,我会要求开发人员包含 ID 属性,这样您的 XPath 就可以抵抗更改。在其他情况下,您可以通过文本定位按钮:
b.find_element_by_xpath("//button[text() = 'Gönder']").click()
尝试 click
与 webdriver wait
使元素成为 clickable
以便元素能够接收 click
.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 40).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-success")))
element.click()
按钮在 HTML 中看起来像这样:
<button type="submit" class="btn btn-success"> <strong>Gönder</strong></button>
我在 python 中使用了这个:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
b = webdriver.Firefox()
b.get("####")
url = b.find_element_by_name("link")
url.send_keys("####")
b.find_element_by_xpath("//button[@type='submit']").click()
还有更多按钮 type="submit"
我只需要调用 1 个特定按钮
有谁知道我怎样才能点击按钮。
如果可能的话,我会要求开发人员包含 ID 属性,这样您的 XPath 就可以抵抗更改。在其他情况下,您可以通过文本定位按钮:
b.find_element_by_xpath("//button[text() = 'Gönder']").click()
尝试 click
与 webdriver wait
使元素成为 clickable
以便元素能够接收 click
.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 40).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-success")))
element.click()