“驱动程序”对象没有属性“ find_element_by_xpath”

时间:2020-07-28 13:24:58

标签: python selenium-webdriver attributeerror

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

错误在脚本的这一部分:

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

登录成功

点击了数据下拉列表

“驱动程序”对象没有属性“ find_element_by_xpath” 数据菜单可见且可单击

在21.552秒内进行了1次测试

让我知道是否需要HTML内容才能解决问题

非常感谢您的帮助,谢谢!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

谢谢您的回复,伙计们!

我最终将View_All的定位器的初始化移出了init方法-效果很好!

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