<div class="col autocomplete theme-standard pin-left" data-control="autocomplete" data-auto-pos="true">
<label class="text autocomplete-arrow icon-before" id="FH-origin-label">
<span class="label">Leaving from</span>
<span class="visuallyhidden">City or airport</span>
<input type="text" name="FrAirport" data-canonic="origin" id="FH-origin" data-minchar="3" data-provide="autocomplete" data-template="#uitk-autocomplete-default" data-theme="autocomplete" data-closetext="Close" data-continuetext="Continue typing to refine search" data-lob="PACKAGES" data-mask="95" data-version="v4" data-locale="en_US" data-forceicon="flights" data-autoselect="touch" data-selectioncallback="publishingWizardPackageTypeAheadOriginCallback" placeholder="City or airport" xpath="1">
<span class="icon icon-location" aria-hidden="true"></span>
</label>
<div class="autocomplete-dropdown"></div>
</div>
我正尝试使用chrome webdriver和while循环,在以下网站上选择“离开”部分之一:https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL
我尝试了以下代码:
package dropdowns;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class synchronization {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\everybody\\Desktop\\selenium\\library\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement a = driver.findElement(By.id("FH-origin"));
a.sendKeys("NEW");
while (!a.equals("New Haven, CT (HVN-All Airports)")) {
//Thread.sleep(4000);
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ENTER);
}
答案 0 :(得分:1)
这是您要找的吗?
while (!a.getText().equals("New Haven, CT (HVN-All Airports)")) {
Thread.sleep(4000); //what is the need of this Thread sleep?
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ARROW_DOWN);
}
driver.findElement(By.id("FH-origin")).sendKeys(Keys.ENTER);
我只是根据我的假设修正了代码
发布HTML可以帮助其他SO用户找到您问题的答案
答案 1 :(得分:0)
在给定页面中,
输入几个字母后,它将自动完成建议加载到列表中。
您必须等待自动完成下拉菜单显示出来。为此,请使用显式等待,因为它会等到元素可见后再显示。
明确等待:
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(autocomplete));
在那之后,您必须找到包含“ New Heaven”的物品,然后单击它。您可以使用XPath查找包含特定文本的元素。
XPath:
//*[@id='backwards']/li/a[contains(.,'New Haven')]
现在您已经找到了该项目,您不必再运行循环就可以找到该元素了。
尝试:
driver.get("https://alaskatrips.poweredbygps.com/g/pt/hotels?MDPCID=ALASKA-US.TPS.BRAND.hotels.HOTEL");
WebElement leavingFrom = driver.findElement(By.id("FH-origin"));
WebElement autocomplete = driver.findElement(By.className("autocomplete-dropdown"));
leavingFrom.sendKeys("NEW");
//after entering sample text wait for the autocomplete drop-down to show up
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(autocomplete));
WebElement newHaevn = driver.findElement(By.xpath("//*[@id='backwards']/li/a[contains(.,'New Haven')]"));
newHaevn.click();
为此,您必须导入
import org.openqa.selenium.support.ui.WebDriverWait;
对于Chrome not reachable
例外,可能的解决方案是:
no-sandbox
chrome选项。 ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox");
chromeDriver = new ChromeDriver(options);