首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当浏览器未显示在selenium中时,无法单击元素

当浏览器未显示在selenium中时,无法单击元素
EN

Stack Overflow用户
提问于 2014-01-14 21:36:48
回答 1查看 161关注 0票数 0

我使用的是selenium web驱动程序2.37.0和java。这是一个简单的点击元素的java程序。

代码语言:javascript
复制
    WebDriver driver = new FirefoxDriver();
    driver.get("http://store.apple.com/us/buy-mac/mac-mini");
    WebElement checkAvailabilityLink = driver.findElement(By.xpath("//div[@class = 'retail-availability-search-trigger-block cold-start']//button"));
    checkAvailabilityLink.click();
    WebElement zipCode = driver.findElement(By.xpath("//input[@id = 'retail-availability-search-query']"));
    zipCode.sendKeys("33180");
    WebElement searchStores = driver.findElement(By.xpath(".//*[@id='retail-availability-search-search-button']"));
    searchStores.click();
    Thread.sleep(5000L);
    WebElement selectStore = driver.findElement(By.xpath(".//*[@id='retail-availability-search-select-store-button']"));
    selectStore.click();

当浏览器显示时,这段代码可以正常工作。当打开的浏览器被最小化/其他窗口被打开时,即使点击了SearchStoreButton,它也不会显示商店。我不明白为什么会有这个问题。

EN

回答 1

Stack Overflow用户

发布于 2018-07-11 16:22:35

这是因为当您最小化窗口或打开另一个选项卡(switching into another window object)时,webdriver无法访问请求窗口中的DOM。

你也可以在#16.3.2上看到how selenium "Emulate the User"。模拟用户

代码语言:javascript
复制
# native-events approach 
Sadly, this approach is only possible where WebDriver can bind closely with 
the browser and where the development team have determined how best to send 
native events without requiring the browser window to be focused (as Selenium 
tests take a long time to run, and it's useful to be able to use the machine 
for other tasks as they run). At the time of writing, this means that native 
events can be used on Linux and Windows, but not Mac OS X.

建议您在使用webdriver进行浏览器自动化时,不要中断自动化过程,只需让它按照您设置的步骤运行即可。

如果希望在运行selenium代码的同一台机器上继续工作,可以使用RemoteWebDriver

以下是一些可以帮助您设置selenium-server的参考资料

当您想要在Linux server.

设置selenium-grid以提供方便、稳定和灵活的环境来运行测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21114920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档