我正在尝试自动将信贷费用提取到Excel工作表中;我设法使登录工作。一旦进入网站,就会出现一个名为“搜索”的按钮。我似乎无法弄清楚如何单击该按钮。
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
感谢您的帮助。
答案 0 :(得分:0)
提交登录后,尝试添加以下代码:
...
...
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
答案 1 :(得分:0)
要使用Selenium将click()
上的<button>
用作 Search ,您需要为element_to_be_clickable()
引出WebDriverWait您可以使用以下任一Locator Strategies:
使用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