我使用的是selenium web驱动程序2.37.0和java。这是一个简单的点击元素的java程序。
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,它也不会显示商店。我不明白为什么会有这个问题。
发布于 2018-07-11 16:22:35
这是因为当您最小化窗口或打开另一个选项卡(switching into another window object)时,webdriver无法访问请求窗口中的DOM。
你也可以在#16.3.2上看到how selenium "Emulate the User"。模拟用户。
# 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以提供方便、稳定和灵活的环境来运行测试。
https://stackoverflow.com/questions/21114920
复制相似问题