gpt4 book ai didi

java - 如何使用 selenium 在下拉列表中选择值,其中值是由谷歌建议的

转载 作者:行者123 更新时间:2023-12-02 05:09:41 24 4
gpt4 key购买 nike

表单中有一个输入文本区域。当我输入名称 city 时,它会使用 google api 给出城市的建议。我该如何选择我的愿望值?

HTML 代码

<input class="focus" autocomplete="off" placeholder="Enter a location" name="msf-city" id="msf-city" type="text">

Google-API suggesting city name according to the enterd Text

最佳答案

下面是与 Google 自动建议搜索字段相关的图片: enter image description here

以下代码将导航到 Google 网站,然后在输入字段中填充文本“stackoverflow”,然后从自动建议框中单击“stackoverflow jobs”:

//Opening browser instance
WebDriver driver = new FirefoxDriver();

//Maximizing browser window
driver.manage().window().maximize();

//Implicit timeout of 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Navigate to google site
driver.get("http://google.co.in/");

//Send text to the input box
driver.findElement(By.id("gbqfq")).sendKeys("stackoverflow");

//Select option from the autosuggest box
driver.findElement(By.xpath("//ul[@role='listbox']//div[.='stackoverflow jobs']")).click();
<小时/>

从您的 html 片段中,我只能将Keys发送到文本字段(如下所示),但不能从自动建议框中选择该选项。

driver.findElement(By.id("msf-city")).sendKeys("some city name");

因此,请为自动建议框中显示的选项添加相关的 HTML 代码段!!

关于java - 如何使用 selenium 在下拉列表中选择值,其中值是由谷歌建议的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27442800/

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