试图单击按钮,但xpath不断出现错误

问题描述 投票:2回答:2

我正在尝试自动将信贷费用提取到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

感谢您的任何帮助。

python selenium xpath css-selectors webdriverwait
2个回答
0
投票

使用click()<button>上的Selenium上的文本显示为搜索,您需要为WebDriverWait引入element_to_be_clickable(),并且可以使用以下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
    

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
© www.soinside.com 2019 - 2024. All rights reserved.