英文:
How to make a WebDriverWait if page changes its content asynchronously (without page reloading)?
问题
// 使用Selenium Webdriver(Java)编写测试代码,将https://cloud.google.com作为驱动程序。
// 首先查找搜索输入字段,然后使用sendKeys("search phrase \n")发送搜索短语。
// 在此之后,页面开始更改其内容,我尝试使用WebDriverWait拦截这些更改:
// 第一个等待 - 等待页面开始通过删除搜索Google图标来更改其内容
new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));
// 第二个等待 - 我在等待新的超链接出现(在整个页面在无需重新加载的情况下异步重新加载后,此超链接将出现在搜索结果中)
new WebDriverWait(driver, 30)
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='https://cloud.google.com/products/calculator']")));
// 关键是,等待不会在元素显示之前等待30秒。代码只是抛出异常:
org.openqa.selenium.NoSuchElementException:
没有这样的元素:无法定位元素:{"method":"xpath","selector":"//a[@href='https://cloud.google.com/products/calculator']"}
非常感谢任何帮助!
英文:
I'm coding test with Selenium Webdriver (Java), getting https://cloud.google.com as a driver.
I start with finding search input field, sendKeys("search phrase \n"). After that page starts changing its content and I'm trying to intersept these changes with WebDriverWait:
// first Wait - is to wait before page starts changing is content by removing search google icon
new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));
//second Wait - i'm waiting new hyperlink to appear (this hyperlink appears in search results after the whole page is asynchronically reloaded without page reloading)
new WebDriverWait(driver,30)
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='https://cloud.google.com/products/calculator']")));
The point is, that Wait doesn't waits for 30 seconds before element shows up. Code just throws an exception:
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href='https:`//cloud.google.com/products/calculator']"}`
Any help will be much appreciated!
答案1
得分: 1
请查看附上的截图。这里的Href链接与您在代码中使用的不同。
您可以使用以下代码:
wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));
英文:
Please check the attached screenshot. Here the Href link is different then you have used in your code.
You can use the below code
wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));
</details>
# 答案2
**得分**: 0
以下是翻译好的内容:
要定位第一个搜索结果,您可以使用以下 xpath;
```xpath
//a[contains(text(),'Google Cloud Platform Pricing')]
检查您的 xpath
您可以从浏览器本身检查您的 xpath 是否正确。
- 进入开发者工具(Ctrl + Shift + I)
- 在“元素”选项卡中,按下 Ctrl + F
- 输入您要检查的 xpath
然后它将显示出该 xpath 是否正确,以及可以从中定位多少个网页元素。
英文:
To locate the first search result you can use the following xpath;
//a[contains(text(),'Google Cloud Platform Pricing')]
Checking your xpath
You can check whether your xpath is correct or not from the browser itself.
- Go to DevTools (Ctril + Shift + I)
- In the 'Elements' tab, press Ctrl + F
- Input the xpath that you want to check
And it will show you whether it is correct and how many web-elements can be located from it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论