【问题标题】:'Driver' object has no attribute 'find_element_by_xpath'“驱动程序”对象没有属性“find_element_by_xpath”
【发布时间】:2020-07-28 13:24:58
【问题描述】:

有人可以看看我在下面分享的脚本,让我知道我可能缺少什么吗?

错误出现在这部分脚本中:

self.driver.find_element_by_xpath(self.view_all_link).click()

单击下拉菜单后,我认为驱动程序无法找到“查看全部”链接 项目“数据”(参考下图)

enter image description here

主类:

   import unittest
    from pageobjects.form_case_exports import FormCaseExports
    from pageobjects.login import Login
    from webdriver import Driver
    from values import inputs
    import time
    
    
    class TestCCHQ(unittest.TestCase):
    
        def setUp(self):
            self.driver = Driver()
            self.driver.navigate(inputs.base_url)
            login_page = Login(self.driver)
            login_page.accept_cookies()
            login_page.enter_username(inputs.login_username)
            login_page.enter_password(inputs.login_password)
            login_page.click_submit()
    
        def test_form_case_exports(self):
            form_case_exports = FormCaseExports(self.driver)
            try:
                form_case_exports.click_data_dropdown()
            except Exception as e:
                print(e)
            finally:
                print("Data Menu Visible and Click-able")
                time.sleep(2)
         
    
        def tearDown(self):
        self.driver.instance.quit()
    
    
    if __name__ == '__main__':
        unittest.main()

form_case_exports 类:

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from values import inputs

class FormCaseExports:

    def __init__(self, driver):  # initialize each WebElement here
        self.driver = driver
        wait = WebDriverWait(self.driver.instance, 10)
        self.data_dropdown = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="ProjectDataTab"]'))) # Data dropdown
        self.view_all_link = "/html/body/div[1]/div[1]/div/nav/ul/li[3]/ul/li[6]/a"
            #wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'View All')))  # View All link



    def click_data_dropdown(self):
        self.data_dropdown.click()
        print("Data dropdown clicked")
        time.sleep(2)
        self.driver.find_element_by_xpath(self.view_all_link).click()
        print("View All link clicked")
        time.sleep(2)

结果:

接受 Cookie

登录成功

点击数据下拉菜单

'Driver' 对象没有属性 'find_element_by_xpath' 数据菜单可见且可点击 .

在 21.552 秒内运行 1 次测试

如果需要 HTML 内容来解决问题,请告诉我

非常感谢任何帮助,谢谢!

【问题讨论】:

    标签: python selenium-webdriver attributeerror


    【解决方案1】:

    可以在两者之间放置 time.sleep(2) 以检查它是否稍后在 init 方法中加载。或者 不要包含在 init 方法中

    【讨论】:

      【解决方案2】:

      请在点击data_dropdown点击后添加可见等待,然后点击等待“查看全部”链接。

      【讨论】:

        【解决方案3】:

        谢谢大家的回复!

        我最终将 View_All 定位器的初始化从 init 方法中移出 - 这工作得很好!

        其他建议的方法听起来也不错 - 将在某个时间点尝试它们:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-01
          • 2022-06-25
          • 2018-11-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-14
          相关资源
          最近更新 更多