問(wèn)題描述
我的腳本代碼中有以下行,我從 Selenium IDE 獲得的 XPath 工作正常:
I have the following line in my script code, where the XPath I got it from Selenium IDE that works fine:
driver.find_element_by_xpath("(//a[contains(@href, '')])[20]").click()
自動(dòng)化測(cè)試在此處停止并出現(xiàn)此錯(cuò)誤:
An automation test stops here with this error:
Traceback (most recent call last):
File "Script.py", line 65, in <module>
driver.find_element_by_xpath("http://a[contains(@href, '')])[20]").click()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)
如何解決這個(gè)問(wèn)題?
感謝您的幫助.
推薦答案
既然你只是想抓取數(shù)據(jù),我推薦你使用這個(gè)方案:
Seeing as you just want to scrape the data, I recommend you use this solution:
element = driver.find_element_by_xpath("(//a[contains(@href, '')])[20]")
driver.execute_script("arguments[0].click();", element)
通過(guò) Javascript 點(diǎn)擊元素,而不是 selenium 使用的自然"點(diǎn)擊(試圖模擬用戶(hù)體驗(yàn)).
Which clicks the element via Javascript as opposed to a "natural" click that selenium uses (to try to simulate the user experience).
我在這里回答了一個(gè)類(lèi)似的問(wèn)題,該問(wèn)題也鏈接到另一個(gè)帖子.
I answered a similar question here that links to another post on it as well.
這篇關(guān)于如何解決此問(wèn)題元素不可交互的 Selenium Python的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!