【问题标题】:Trying to click a button but keep getting an error with xpath试图单击一个按钮,但不断收到 xpath 错误
【发布时间】:2020-09-24 02:30:30
【问题描述】:

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


    【解决方案1】:

    登录提交后,尝试添加以下代码:

    ...
    ...
    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
    

    【讨论】:

      【解决方案2】:

      &lt;button&gt; 上的click(),文本为搜索,使用Selenium,您需要为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
        

      【讨论】:

      • 感谢所有帮助 './span[text()='Search'' 对 Xpath 有什么作用?我之前只在课堂上尝试过,但没有奏效。我猜 span[...] 是缺少的组件;好奇它是如何工作的?
      • @newbie_khan //span[text()='Search'] 在父 &lt;button&gt; 元素内的 &lt;span&gt; 元素内查找文本 Search。很高兴能为您提供帮助。
      • QQ:我如何在下面的 html 代码中访问 5 月 27 日的文本?
      • 另外,点击所有搜索按钮后,我是否可以使用驱动程序访问网页上的新内容,还是必须做一些事情才能在驱动程序中加载新内容?
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签