gpt4 book ai didi

java - Selenium 测试在谷歌反向查找时自动完成失败

转载 作者:搜寻专家 更新时间:2023-11-01 00:54:52 27 4
gpt4 key购买 nike

我缺少一些非常基本的东西。我想,无论我能收集到什么,我都需要在我键入时下拉列表的 id。获得 id 后,我想我可以获取列表并遍历它。

自动完成的 Web 代码是:

<div class="col-md-12">
<label>Google Map Location*</label>
<input id="searchTextField" type="text" class="form-control" placeholder="Enter a location" autocomplete="on" required="required">
<input id="latitude" type="text" style="display: none;" class="form-control" value="">
<input id="longitude" type="text" style="display: none;"class="form-control" value="">
<p class="help-block" style="color: #737373;">Instruction: Please drag the marker to get exact location of the IT park.</p>
</div>

初始化js代码的javascript是

        var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);

Selenium 代码是:

autocompleteElement = God.getWebElementById("searchTextField");
autocompleteElement.sendKeys("Airport");
List<WebElement> autoCompleteList = Initialize.getInstance().getDriver().findElements(
By.className("form-control");
for (WebElement autocompleteItem : autoCompleteList) {
if (autocompleteItem.getText().contains("Pune Airport")) {
autocompleteItem.click();
}
break;
}

我收到 autocompleteItem is null or empty。

编辑 1

我是 javascript 的新手,我继承了这个代码库。我不明白的是如何将 locatorid 分配给容器,以便我可以遍历 WebElement 并执行点击我想要的结果。

编辑2

我添加了自动完成检查元素的快照。下面的答案指的是 xpath(“对我来说非常复杂的东西”)。在我的特定情况下,我应该用什么替换 xpath?

您需要什么帮助我获取下拉列表列表?

snapshot of the autocomplete dropdown

编辑3

我像这样使用类名 form-control

List<WebElement> autoCompleteList = waitForElementByClass(
"form-control");
System.out.println("autocomplete list size " + autoCompleteList.size());
for (WebElement autocompleteItem : autoCompleteList) {
if (autocompleteItem.getText().contains("Wadgaon Sheri")) {
System.out.println("auto complete selected " + autocompleteItem.getText());
autocompleteItem.click();
break;
} else
System.out.println("no match, tagname:" + autocompleteItem.getTagName() + "point:"
+ autocompleteItem.getLocation());
}

并得到以下输出。请注意 autocompleteItem.getText 返回了一个空字符串。

autocomplete list size 16
no match, tagname:inputpoint:(255, 200)
no match, tagname:selectpoint:(255, 274)
no match, tagname:inputpoint:(255, 433)
no match, tagname:selectpoint:(255, 509)
no match, tagname:inputpoint:(255, 553)
no match, tagname:inputpoint:(330, 553)
no match, tagname:inputpoint:(255, 627)
no match, tagname:textareapoint:(525, 200)
no match, tagname:textareapoint:(525, 324)
no match, tagname:inputpoint:(525, 450)
no match, tagname:inputpoint:(525, 526)
no match, tagname:inputpoint:(525, 602)
no match, tagname:inputpoint:(525, 678)
no match, tagname:inputpoint:(796, 200)
no match, tagname:inputpoint:(0, 0)
no match, tagname:inputpoint:(0, 0)

我现在正在尝试使用类名 pac-container pac.logo 然后我也会尝试使用 div.pac-container.pac-logo :)。请记住,我的 javascript 技能不是太好。

pac-container pac.logo

编辑 4现在,我尝试将类名设为 pac-container pac.logopac-container.pac-logodiv.pac-container.pac.logopac-container

的结果

wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(byclass)));

是代码卡在这里吗

如果我将行替换为

wait.until(ExpectedConditions.elementToBeSelected(God.getWebElementByClassNae(byclass)));

我得到一个 NoElementFoundException

所以我最接近解决这个问题的方法是找到该死的下拉列表的类名。

public List<WebElement> waitForElementByClass(String byclass) {
WebDriverWait wait = new WebDriverWait(God.getCurrentBrowser(), 2000);
List<WebElement> elements = null;
boolean waitForElement = true;
System.out.print("Waiting for " + byclass);
do {
System.out.print(".");
try {
//wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(byclass))); wait.until(ExpectedConditions.elementToBeSelected(God.getWebElementByClassNae(byclass)));
waitForElement = false;
System.out.println("found");
elements = God.getWebElementsByClassName(byclass);
} catch (NoSuchElementException e) {
waitForElement = true;
} catch (TimeoutException e) {
waitForElement = false;
return null;
}
} while (waitForElement);
return elements;
}

最佳答案

使用 WebDriverWait 插入代码以等待元素可见。下面的示例,

WebDriverWait wait = new WebDriverWait(driver,10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("form-control")));

关于java - Selenium 测试在谷歌反向查找时自动完成失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805021/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com