当前位置: 首页 > 知识库问答 >
问题:

错误:“元素当前不可见,因此可能无法与selenuim交互”

云焱
2023-03-14

我想抓取谷歌playstore搜索结果完全呈现的网页。

完全呈现的页面具有所有搜索项,而未呈现的页面仅具有20项。(请参阅https://play.google.com/store/search?q=best

我试图用selenium抓取页面,但得到了下面的错误信息。

Traceback (most recent call last):
File "play_test_2.py", line 25, in test_play_test2
driver.find_element_by_id("show-more-button").click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 65, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 385, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///var/folders/8_/n90htn1d0_j4h7l9yt04chl80000gn/T/tmpErWdUz/extensions/fxdriver@googlecode.com/components/command-processor.js:8959:5)
at DelayedCommand.prototype.checkPreconditions_ (file:///var/folders/8_/n90htn1d0_j4h7l9yt04chl80000gn/T/tmpErWdUz/extensions/fxdriver@googlecode.com/components/command-processor.js:11618:1)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/8_/n90htn1d0_j4h7l9yt04chl80000gn/T/tmpErWdUz/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:11)
at fxdriver.Timer.prototype.setTimeout/<.notify          (file:///var/folders/8_/n90htn1d0_j4h7l9yt04chl80000gn/T/tmpErWdUz/extensions/fxdriver@googlecode.com/components/command-processor.js:548:5)

以下代码由Selenuim IDE编写。

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class PlayTest2(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://play.google.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_play_test2(self):
        driver = self.driver
        driver.get(self.base_url + "/store/search?q=best&c=apps")
        driver.find_element_by_id("gbqfb").click()
        driver.find_element_by_id("show-more-button").click()
        driver.find_element_by_id("show-more-button").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

我认为出现错误是因为谷歌商店搜索结果页面有“显示更多”按钮,当它满足某些特定条件时会显示,例如向下和向上滚动,然后再次向下滚动。

如何解决这个问题并抓取google搜索结果页面?

共有1个答案

闻人越
2023-03-14

test_play_test2呼叫

driver.find_element_by_id(显示更多按钮)

两次。< br >我认为在第一次单击之后,selenium无法找到相同的元素,因此失败了。只需从这个方法中删除最近的一行。

更新:你完全正确,按钮的问题是在页面滚动后才出现的。因此,我们必须滚动窗口。以下javascript命令很有帮助

((JavascriptExecutor) driver).executeScript(“window.scrollTo(0,document.body.scrollHeight);”);

一个命令是不够的,我们已经做了好几次:

for (int i=0; i<5; i++ ){
    ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight);");
    try{
        (new WebDriverWait(driver, 5/*sec*/))
                        .until(ExpectedConditions.visibilityOf(element));
        break;
    }
    catch (org.openqa.selenium.TimeoutException e){
    }
}
if(element.isDisplayed()){
    element.click();
}

抱歉,我在这个例子中使用了Java代码,但我希望这能让你明白。

 类似资料:
  • 我需要选择列表中的最后一项。下面的代码显示了该元素当前不可见的消息。如何解决这个问题? HTML: 列表的屏幕截图。该列表有一个搜索字段,用户可以在其中输入前缀以缩小搜索范围。

  • 我正在编写脚本,使用selenium将我的ssh密钥添加到bitket的部署密钥中。直到行 工作正常,但当弹出窗口出现时,我想在特定字段中输入键 它抛出此错误 元素当前不可见,因此可能无法与之交互。我在谷歌上搜索了这个。我开始知道,首先我需要转到此弹出窗口,然后我将能够将值传递给相应的元素。我不知道该怎么做。 请帮助我如何专注于新的弹出窗口。我也使用了时间睡眠(10),但它仍然不适合我。

  • 我正在使用Selenium记录我在网页上的操作,但是,当我运行测试用例时,出现了一个点击操作抛出错误:元素当前不可见,因此可能无法与之交互。但是,我确信按钮是可见的,下面是它的html: 此外,这是我的Selenium IDE测试脚本: 有人知道为什么吗?提前感谢!

  • 问题内容: 我正在尝试单击具有文本克隆概念的范围。以下是html 我使用的javascript代码是: 我确认这是通过Firepath的元素的正确选择。 我还确保按照链接可见该元素。如何强制SeleniumWebDriver单击当前不可见的元素? 这是 计算的CSS 还尝试了以下代码: 异常: org.openqa.selenium.ElementNotVisibleException:元素当前不

  • 问题内容: 我正在尝试在Selenium Web驱动程序脚本下执行,但是几次(并非所有时间)都出现错误。有时在循环中第一次迭代,有时在2次迭代中,有时没有启动循环。它打印所有可用项目的计数正确,但是乳清试图单击项目,显示 问题答案: 终于这对我有用。元素当前不可见,因此可能无法与之交互。 最初,这就像测试只成功了5次中的2次一样。不确定有时如何运作,其他人则无法运作。 通过减少IE中的安全设置来工

  • 我正在尝试在下面执行硒Web驱动程序脚本,但是我正在获得(不是所有时间)。有时在第一次迭代的循环中,有时在2次迭代中,有时没有启动循环。它正确打印所有可用的项目计数,但乳清试图点击项目,它显示