简体   繁体   English

Python-Selenium Web驱动程序错误-self._driver.execute-AttributeError:“ unicode”对象没有属性“ id”

[英]Python - Selenium Web Driver error - self._driver.execute - AttributeError: 'unicode' object has no attribute 'id'

I found an answer here but my code already does what this suggested and still produces the same error so I'm hoping for another answer. 我在这里找到了答案,但是我的代码已经执行了建议的操作,并且仍然会产生相同的错误,因此我希望找到另一个答案。

This is my code that calls ActionChains: 这是我调用ActionChains的代码:

    elif first_col_value == first_col_in_assign:
        res2, assign_string = assign_cmd(spreadsheet_name, row)
        print "Got to main 8 - res2/cmd_string: %s %s" % (res2, assign_string)
        # assign_string2 = u"search_field = driver.find_element_by_name(“q”)"
        if not res2:
            exit(False)
        else:
            action = webdriver.ActionChains(driver).move_to_element(assign_string)
            action.perform()
            continue

This is the what the assign_string looks like built from the spreadsheet: 这是从电子表格构建的assign_string的样子:

    In assign_cmd - param1 = %s search_field
    In assign_cmd - param2 = %s driver.find_element_by_name
    In assign_cmd - param3 = %s “q”
    In assign_cmd - param4 = %s #
    Got to main 8 - res2/assign_string: True search_field = driver.find_element_by_name(“q”)

and this is the error: 这是错误:

    Traceback (most recent call last):
      File "/home/susan/PycharmProjects/justPython/test1.py", line 397, in <module>
      action.perform()
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 70, in perform
action()
      File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/action_chains.py", line 215, in <lambda>
      self._driver.execute(Command.MOVE_TO, {'element': to_element.id}))
      AttributeError: 'unicode' object has no attribute 'id'

      Process finished with exit code 1

I tried putting the unicode string directly into my code that is the commented out line above but it produces the same error. 我尝试将unicode字符串直接放入上面注释行中的代码中,但是会产生相同的错误。 I am stuck and really appreciate any help you can give me. 我很固执,非常感谢您能给我任何帮助。 Many thanks. 非常感谢。

move_to_element() assumes you are passing in an element found before, not a string: move_to_element()假设您传入的是之前找到的元素,而不是字符串:

move_to_element(to_element) move_to_element(to_element)

Moving the mouse to the middle of an element. 将鼠标移到元素的中间。

For example: 例如:

element = driver.find_element_by_id('myid')
action = webdriver.ActionChains(driver).move_to_element(element)
action.perform()

If you have control over the incoming spreadsheet configuration, I'd reorganize it a bit. 如果您可以控制传入的电子表格配置,我将对其进行一些重组。 Instead of having find_element_by_* strings, I'd have two things for each element: a type of locator and a locator itself, eg: 代替使用find_element_by_*字符串,每个元素有两件事:一种定位器和一种定位器本身,例如:

|   type   |   value            |
|   xpath  |  //div[@id="test"] |
|   name   |  q                 |
...

Then, in your tests, you can use find_element() method that receives exactly a type of locator and a value: 然后,在测试中,您可以使用find_element()方法来接收确切的定位符类型和值:

 locator_type, locator_value = get_locator_from_spreadsheet(...)
 element = driver.find_element(locator_type, locator_value)

 action = webdriver.ActionChains(driver).move_to_element(element)
 action.perform()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Python 错误'对象没有属性驱动程序' - Selenium Python error 'object has no attribute driver' pycharm- python 与 selenium web 驱动程序。 得到错误 object has no attribute driver - pycharm- python with selenium web driver. Get error object has no attribute driver AttributeError:“驱动程序”对象没有属性“ id” - AttributeError: 'Driver' object has no attribute 'id' AttributeError: 'list' object 没有属性 'find_element' - Selenium 驱动程序 - AttributeError: 'list' object has no attribute 'find_element' - Selenium driver 抛出错误:AttributeError:&#39;KenLogin&#39;对象没有属性&#39;driver&#39; - Throwing error : AttributeError: 'KenLogin' object has no attribute 'driver' AttributeError:“WebElement”object 没有属性“驱动程序” - AttributeError: 'WebElement' object has no attribute 'driver' 为什么我会收到此错误:AttributeError: 'Fetch_Info' object has no attribute 'driver' 当我运行我的 selenium_web.py 脚本时 - Why do I get this error :AttributeError: 'Fetch_Info' object has no attribute 'driver' when I run my selenium_web.py script AttributeError: 'TestTomsLogin' object 没有属性 'driver' - AttributeError: 'TestTomsLogin' object has no attribute 'driver' AttributeError:&#39;LoginPage&#39;对象没有属性&#39;driver&#39; - AttributeError: 'LoginPage' object has no attribute 'driver' AttributeError:&#39;TestLogin&#39;对象没有属性&#39;driver&#39; - AttributeError: 'TestLogin' object has no attribute 'driver'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM