簡體   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'

我在這里找到了答案,但是我的代碼已經執行了建議的操作,並且仍然會產生相同的錯誤,因此我希望找到另一個答案。

這是我調用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

這是從電子表格構建的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”)

這是錯誤:

    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

我嘗試將unicode字符串直接放入上面注釋行中的代碼中,但是會產生相同的錯誤。 我很固執,非常感謝您能給我任何幫助。 非常感謝。

move_to_element()假設您傳入的是之前找到的元素,而不是字符串:

move_to_element(to_element)

將鼠標移到元素的中間。

例如:

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

如果您可以控制傳入的電子表格配置,我將對其進行一些重組。 代替使用find_element_by_*字符串,每個元素有兩件事:一種定位器和一種定位器本身,例如:

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

然后,在測試中,您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM