【问题标题】:Selenium - AttributeError: object has no attribute 'find_element_by_css_selector'Selenium - AttributeError:对象没有属性'find_element_by_css_selector'
【发布时间】:2018-06-08 07:43:08
【问题描述】:

我想做一个简单的测试脚本,并决定使用亚马逊来试用我的脚本。以下是我的代码:

import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains


class PurchaseEbook(unittest.TestCase):

    def test_setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("https://www.amazon.com/")

    def test_selectOptionFromDDL(self):
        self.ddl_Dept = self.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)")
        self.ddl_Book = self.find_element_by_css_selector("span.nav-hasPanel:nth-child(9) > span:nth-child(1)")

        action = ActionChains(self)
        action.move_to_element(self.ddl_Dept)
        action.move_to_element(self.ddl_Book)
        action.click("div.nav-template:nth-child(8) > div:nth-child(4) > a:nth-    child(1) > span:nth-child(1)")
        action.perform()

    def test_serachKeyword(self):
        element = self.find_element_by_css_selector("#nav-search")
        element.send_keys("Simon Sinek")
        element.submit()
        element.clear()

    def test_tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()

以下是我的错误日志:

错误 [0.000931s]: test_selectOptionFromDDL (ma​​in.PurchaseEbook)

Traceback(最近一次调用最后一次): 文件“amazon-test-script.py”,第 16 行,在 test_selectOptionFromDDL self.ddl_Dept = self.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)") AttributeError:“PurchaseEbook”对象没有属性“find_element_by_css_selector”

================================================ ========================

错误 [0.000000s]: test_serachKeyword (ma​​in.PurchaseEbook)

Traceback(最近一次调用最后一次): 文件“amazon-test-script.py”,第 26 行,在 test_serachKeyword 中 element = self.find_element_by_css_selector("#nav-search") AttributeError:“PurchaseEbook”对象没有属性“find_element_by_css_selector”

================================================ ========================

错误 [0.001004s]: test_tearDown (ma​​in.PurchaseEbook)

Traceback(最近一次调用最后一次): 文件“amazon-test-script.py”,第 32 行,在 test_tearDown self.driver.quit() AttributeError:“PurchaseEbook”对象没有属性“驱动程序”

【问题讨论】:

  • 你能添加相关的html或者告诉你在找哪个元素吗?
  • 您在所有查找元素语句中都缺少 .driver。作为戈兰的回答。它应该是 self.driver.fine_element 而不是 self.find_element。

标签: python selenium


【解决方案1】:

应该是:

self.ddl_Dept = self.driver.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)") 

【讨论】:

  • 不错的收获 +1 !!!关于解决方案的几句话将对未来的读者了解您的答案如何解决问题非常有帮助
猜你喜欢
  • 2022-09-29
  • 2023-01-07
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 2016-07-21
  • 2017-01-14
  • 2022-01-27
相关资源
最近更新 更多