【问题标题】:Problem in finding the element using the xPath using selenium in python在 python 中使用 selenium 使用 xPath 查找元素的问题
【发布时间】:2020-03-04 05:55:55
【问题描述】:

我想登录页面并单击位于网站顶部的按钮,但是当我运行代码时它说找不到元素,下面是我的 python 代码:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://go.xero.com/Dashboard")
user_name = driver.find_element_by_id("email")
user_name.send_keys("mnb@allied.ae")
password = driver.find_element_by_id("password")
password.send_keys()
Submit = driver.find_element_by_id("submitButton")
Submit.click()
CS = driver.find_element_by_xpath("//span[@class='xrh-appbutton--text']")
cs.click()

driver.quit() 

下面是HTML源代码:

【问题讨论】:

    标签: python selenium selenium-webdriver automated-tests


    【解决方案1】:

    似乎需要一些时间才能找到元素。要管理它,您需要在脚本中实现等待机制。使用隐式或显式等待。参考this

    隐式等待

    driver.get("https://go.xero.com/Dashboard")
    driver.implicitly_wait(15)
    

    显式等待

    cs = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='xrh-appbutton--text']")))
    cs.click()
    

    需要为此导入以下包 -

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait
    

    【讨论】:

    • 感谢它解决了我的问题
    猜你喜欢
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多