我遗漏了一些非常基本的东西.从我能收集到的任何东西,我想,我需要在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>
Run Code Online (Sandbox Code Playgroud)
用于初始化js代码的javascript是
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
我得到的autocompleteItem是null或空.
编辑1
我是新手javascript,我继承了这个代码库.我无法理解的是如何为容器分配locatorOR id,以便我可以通过迭代WebElement并执行click我想要的结果.
编辑2
我添加了自动完成检查元素的快照.下面的答案指的是xpath("对我来说非常复杂").在我的具体情况下,我该如何替换xpath?
要么
您需要什么来帮助我获取下拉列表?
编辑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());
}
Run Code Online (Sandbox Code Playgroud)
并得到以下输出.请注意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)
Run Code Online (Sandbox Code Playgroud)
我现在正在尝试使用类名pac-container pac.logo,然后我也会尝试使用div.pac-container.pac-logo:).请记住,我的javascript技能不是太好.
编辑4
现在,我已经尝试过的类名pac-container pac.logo,pac-container.pac-logo,div.pac-container.pac.logo和pac-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;
}
Run Code Online (Sandbox Code Playgroud)
小智 0
使用 WebDriverWait 插入代码以等待元素可见。下面的示例,
WebDriverWait wait = new WebDriverWait(driver,10000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("form-control")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |