最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Selenium已通过ID获得元素后click出错:element is not attached to the page document

selenium crifan 912浏览 0评论

折腾:

【已解决】Selenium给输入框send_keys无法输入文字

期间,对于:

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

logging.info(“searchInputElement=%s”, searchInputElement)

searchInputElement.click()

searchInputElement.clear()

searchInputElement.send_keys(toSearch)

在已经通过ID获得了element元素后,结果

searchInputElement.click()

出错:

    searchInputElement.click()

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”, line 80, in click

    self._execute(Command.CLICK_ELEMENT)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py”, line 501, in _execute

    return self._parent.execute(command, params)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 308, in execute

    self.error_handler.check_response(response)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 194, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

  (Session info: chrome=61.0.3163.100)

  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64)

selenium click element is not attached to the page document

Selenium – Stale element reference: element is not attached to the page document in C# – Stack Overflow

java – Selenium ERROR : stale element reference: element is not attached to the page document – Stack Overflow

Python with Selenium “element is not attached to the page document” – Stack Overflow

没解决办法啊。

Stale Element Reference Exception

java – Selenium : stale element reference: element is not attached to the page document – Stack Overflow

c# – Selenium – stale element reference: element is not attached to the page – Stack Overflow

换成:

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

logging.info(“searchInputElement=%s”, searchInputElement)

driver.find_element_by_id(“ctl00_hdr_hss_searchBox”).click()

结果出现:

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input name=”ctl00$hdr$hss$searchBox” id=”ctl00_hdr_hss_searchBox” class=”search-box display-inline store-search-box”

java – stale element reference: element is not attached to the page document – Stack Overflow

再去加上try catch重新试试

try:

    searchInputElement = WebDriverWait(driver, 10).until(

        EC.presence_of_element_located((By.ID, “ctl00_hdr_hss_searchBox”)))

    logging.info(“searchInputElement=%s”, searchInputElement)

    searchInputElement.click()

    searchInputElement.clear()

    searchInputElement.send_keys(toSearch)

except:

    logging.error(“Can not find befrugal search input after login”)

    # driver.quit()

    # try again

    searchInputElement2 = WebDriverWait(driver, 10).until(

        EC.presence_of_element_located((By.ID, “ctl00_hdr_hss_searchBox”)))

    logging.info(“searchInputElement2=%s”, searchInputElement2)

    searchInputElement2.click()

    searchInputElement2.clear()

    searchInputElement2.send_keys(toSearch)

结果问题依旧。

考虑到:

登录前和登录后,页面刷新了,所以:

重新获取一次页面的内容:

https://www.befrugal.com/home/

或许就可以了。

然后的确可以了,可以输入字符串了:

同时,去掉之前的:

# searchInputElement.click()

# searchInputElement.clear()

也是可以正常的send_keys的。

【总结】

此处,之所以:

已经通过ID获得了对应的元素:

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

并且不是空,但是

searchInputElement.click()

却出现:

element is not attached to the page document

的错误。

根本原因是:

由于最开始访问的网址:

https://www.befrugal.com/home/

在输入了用户名和密码,点击登录后,页面刷新了

所以导致接下来获得的(原先的html中的)元素,都是无效的。

解决办法:

在继续获取元素之前,刷新一下页面:

driver.get(befrugalHomeUrl)

即可。

接着所有的获取元素后再操作:

screenshotAndSave(“befrugal_hasLoginMyAccount.png”)

toSearch = gCfg[“befrugal”][“search”]

searchInputElement = driver.find_element_by_id(“ctl00_hdr_hss_searchBox”)

logging.info(“searchInputElement=%s”, searchInputElement)

searchInputElement.send_keys(toSearch)

就都正常了:

  • 截的图,也是登录后的页面的截图了

    • 之前还觉得奇怪,为何截图是旧的没有登录的

  • find_element_by_id获取的元素也都是有效的了

    • 后续的click(), clear(), send_keys() 也都正常了

【后记】

想到了,selenium中,是否应该有 刷新 之类的功能

selenium refresh

selenium – Refreshing web page by WebDriver when waiting for specific condition – Stack Overflow

通过自动完成:

直接得到有:

<code>driver.refresh()
</code>

所以试试,果然是可以的,比之前的:

<code># driver.get(befrugalHomeUrl)
</code>

效果更好,含义更清晰。

【总结】

如果selenium中Python想要实现页面刷新,则可以使用:

<code>driver.refresh()
</code>

注:

别人提到的:

<code>driver.findElement(By.name("s")).sendKeys(Keys.F5);
</code>

或:

<code>driver.get(driver.getCurrentUrl());
</code>

或:

<code>driver.navigate().to(driver.getCurrentUrl());
</code>

或:

<code>driver.findElement(By.name("s")).sendKeys("\uE035");
</code>

有机会都可以去试试。

另:

Python selenium —— 控制浏览器前进(forward)、后退(back)、刷新(refresh) « 灰蓝

其他的按钮,以后估计也会用到:

<code>driver.back()
driver.forward()
</code>

转载请注明:在路上 » 【已解决】Selenium已通过ID获得元素后click出错:element is not attached to the page document

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
13 queries in 0.219 seconds, using 10.33MB memory