简体   繁体   中英

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:

    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:

    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. 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(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:

|   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:

 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM