如何在 selenium 中找到按钮?(Python)
How to find a button in selenium?(Python)
我想在 selenium 中找到这个按钮:
<input class="button button-primary" type="submit" value="Send me the code" data-type="save">
我试过这个:
driver.find_element_by_xpath("//input[@value='Send me the code']").click()
但这没有用。
像 driver.find_element_by_xpath("//input[@value='Send me the code']").click()
这样的简单代码行可能无法正常工作的原因有很多。
最可能的原因可能是缺少等待/延迟。您应该等到元素完全加载后再尝试访问它。而不是使用 driver.find_element_by_xpath("//input[@value='Send me the code']").click()
请尝试这样的事情:
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@value='Send me the code']"))).click()
要使用它,您需要导入以下内容:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
并初始化wait
对象
wait = WebDriverWait(driver, 20)
也有可能定位器不是唯一的。
该元素也可以位于 iframe 等内部。
Selenium中基本上有4种点击方式。
我将使用这个 xpath
//input[@value='Send me the code']
代码试用 1 :
time.sleep(5)
driver.find_element_by_xpath("//input[@value='Send me the code']").click()
代码试用 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Send me the code']"))).click()
代码试用 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//input[@value='Send me the code']")
driver.execute_script("arguments[0].click();", button)
代码试用 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//input[@value='Send me the code']")
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 : 如果我们有 请检查 dev tools
(Google chrome)是否在 HTML DOM
中唯一 个条目。
检查步骤:
Press F12 in Chrome
-> 转到 element
部分 -> 执行 CTRL + F
-> 然后粘贴 xpath
并查看是否需要 element
正在 突出显示 与 1/1
匹配节点。
我想在 selenium 中找到这个按钮:
<input class="button button-primary" type="submit" value="Send me the code" data-type="save">
我试过这个:
driver.find_element_by_xpath("//input[@value='Send me the code']").click()
但这没有用。
像 driver.find_element_by_xpath("//input[@value='Send me the code']").click()
这样的简单代码行可能无法正常工作的原因有很多。
最可能的原因可能是缺少等待/延迟。您应该等到元素完全加载后再尝试访问它。而不是使用 driver.find_element_by_xpath("//input[@value='Send me the code']").click()
请尝试这样的事情:
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@value='Send me the code']"))).click()
要使用它,您需要导入以下内容:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
并初始化wait
对象
wait = WebDriverWait(driver, 20)
也有可能定位器不是唯一的。
该元素也可以位于 iframe 等内部。
Selenium中基本上有4种点击方式。
我将使用这个 xpath
//input[@value='Send me the code']
代码试用 1 :
time.sleep(5)
driver.find_element_by_xpath("//input[@value='Send me the code']").click()
代码试用 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Send me the code']"))).click()
代码试用 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//input[@value='Send me the code']")
driver.execute_script("arguments[0].click();", button)
代码试用 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//input[@value='Send me the code']")
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 : 如果我们有 请检查 dev tools
(Google chrome)是否在 HTML DOM
中唯一 个条目。
检查步骤:
Press F12 in Chrome
-> 转到 element
部分 -> 执行 CTRL + F
-> 然后粘贴 xpath
并查看是否需要 element
正在 突出显示 与 1/1
匹配节点。