尝试单击按钮但 xpath 不断出错
Trying to click a button but keep getting an error with xpath
我正在尝试自动将信用费用提取到 excel sheet;我已经设法让登录正常工作。进入网站后,会出现一个标题为 "Search" 的按钮。我似乎无法弄清楚如何单击该按钮。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[@id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[@id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[@id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html标签如下
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath如下
//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
感谢任何帮助。
提交登录后,尝试添加以下代码:
...
...
driver.find_element_by_xpath('//*[@id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-fluid']//span[text()='Search']"))).click()
以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
到 click()
在 <button>
上使用文本 搜索 使用 you need to induce for the element_to_be_clickable()
and you can use either of the following :
使用CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
使用XPATH
:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-fluid']/span[text()='Search']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
我正在尝试自动将信用费用提取到 excel sheet;我已经设法让登录正常工作。进入网站后,会出现一个标题为 "Search" 的按钮。我似乎无法弄清楚如何单击该按钮。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import time
chromedriver = "C:/Python_Ex/chromedriver_win32/chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
delay = 30
driver.get("https://global.americanexpress.com/activity/date-range?from=2020-05-01&to=2020-05-30")
driver.find_element_by_xpath('//*[@id="eliloUserID"]').send_keys("removed")
driver.find_element_by_xpath('//*[@id="eliloPassword"]').send_keys("removed")
driver.find_element_by_xpath('//*[@id="loginSubmit"]').click()
time.sleep(10)
#print(driver.find_elements_by_xpath('//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button'))
search_button = driver.find_elements_by_xpath('//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button')
search_button.click()
html标签如下
<button class="btn btn-fluid" tabindex="0" type="button"> <span>Search</span></button>
Xpath如下
//*[@id="root"]/div[1]/div/div[2]/div/div/div[5]/div/div[3]/div/div/div/div[2]/div[2]/div/div/div/div/div[1]/section/div[4]/div[2]/button
感谢任何帮助。
提交登录后,尝试添加以下代码:
...
...
driver.find_element_by_xpath('//*[@id="loginSubmit"]').click()
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-fluid']//span[text()='Search']"))).click()
以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
到 click()
在 <button>
上使用文本 搜索 使用 element_to_be_clickable()
and you can use either of the following
使用
CSS_SELECTOR
:WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-fluid[type='button']>span"))).click()
使用
XPATH
:WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-fluid']/span[text()='Search']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC