本文介绍了如何“点击”关于使用Selenium在Amazon上进行自动建议的某些建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试自动执行amazon.com上的自动完成建议。但是与Google搜索选项不同,建议的xpath总是在变化。我发布的代码并非每次都起作用,因为有时所需建议的xpath / id / cssselector发生了变化(@ id = \ issDiv8\],有时是 issDiv4或 issDiv6,依此类推。
I am trying to automate auto complete suggestions on amazon.com. But unlike google search options, xpath of suggestions is always changing. The code I posted doesn't work every time because sometimes the xpath/id/cssselector of the desired suggestion is changing (@id=\"issDiv8\"] sometimes it is "issDiv4" or "issDiv6" and so on.
WebElement searchTextField = driver.findElement(By.id("twotabsearchtextbox"));
searchTextField.sendKeys("turbo");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@id=\"issDiv8\"]")));
List<WebElement> autoSuggest = driver.findElements(By.xpath("//*[@id=\"issDiv8\"]"));
System.out.println("Auto Suggest List ::" + autoSuggest.size());
for (int i = 0; i < autoSuggest.size(); i++) {
System.out.println(autoSuggest.get(i).getText());
if (autoSuggest.get(i).getText().equals("turbotax")) {
autoSuggest.get(i).click();
System.out.println("Success");
break;
推荐答案
使用 WebdriverWait
处理动态元素并使用以下 xpath
Use WebdriverWait
to handle dynamic element and use the following xpath
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@data-keyword='turbotax']")));
element.click()
这篇关于如何“点击”关于使用Selenium在Amazon上进行自动建议的某些建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!