Selenium 发送键(文本),从下拉列表中选择并回车
Selenium send keys (text), selecting from dropdown and hit enter
我正在尝试搜索此网站:http://www.jackson-stops.co.uk/
URL 中没有显示数据,所以我正在使用 chromedriver。
我的代码是:
public static void main(String[] args) {
//setup chromedriver
File file = new File("C:\Users\USER\Desktop\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.jackson-stops.co.uk/");
//begin the simulation
WebElement menu = driver.findElement(By.xpath("//*[@id=\"sliderLocation\"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", menu);
menu.sendKeys("Knightsbridge");
Thread.sleep(4000);
Select menu2 = new Select(menu);
menu2.selectByVisibleText("Knightsbridge");
Thread.sleep(4000);
} catch (Exception exp) {
System.out.println("exception:" + exp);
//close and quit windows
driver.close();
driver.quit();
}
//close and quit windows
driver.close();
driver.quit();
}
我得到的错误是:
exception:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
我如何才能 select 一个位置并按回车键,因为我尝试检查 HTML 并且选项是动态加载的,所以不可见!
您正在尝试 select 一个元素,但它不是 select 列表,它是一个 link,因此您所要做的就是单击该元素,仅此而已
首先传值
driver.findElement(By.xpath("//*[@id='sliderLocation']")).sendKeys("Knightsbridge")
一旦完成,它就会填充值,所以你需要点击其中一个选项,所以你可以直接点击这个元素(因为这个填充需要时间,你需要使用隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS))
然后写
driver.findElement(By.xpath("//a[text()='Knightsbridge, London']")).click()
或者如果你想选择由Knightsbridge
组成的元素然后写下面的代码这将选择第一个由Knightsbridge
组成的选项然后写
driver.findElement(By.xpath("//a[contains(text(),'Knightsbridge']")).click()
您不必在 selenium 代码中的任何地方使用 sleep 语句,selenium 会在点击后自动等待,直到一切正常。一个例外情况是,如果您的页面在将值放入文本框后刷新(select_list不需要),那么您需要使用隐式等待,否则甚至不需要隐式等待。
上面的代码我从Ruby转换成Java,我用来检查的原始代码来自selenium Ruby绑定,代码如下
@driver.find_element(:xpath, "//*[@id='sliderLocation']").send_keys "Knightsbridge"
@driver.manage.timeouts.implicit_wait = 10
@driver.find_element(:xpath, "//a[contains(text(),'Knightsbridge')]").click
@driver.find_element(:xpath, "//a[text()='Knightsbridge, London']").click
您可以进行以下操作:
String requiredCity = "London";
List<WebElement> menu2 = driver.findElements(By.xpath("//ul[@id='ui-id-3']/li"));
System.out.println("Total options: "+menu2.size());
for(int i=0;i<menu2.size();i++)
{
String CurrentOption = menu2.get(i).getText();
if(CurrentOption.contains(requiredCity)){
System.out.println("Found the city : "+CurrentOption);
menu2.get(i).click();
}
}
这是使用 Ranjeet 回答的完整解决方案。
File file = new File("C:\Users\USER\Desktop\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.jackson-stops.co.uk/");
//begin the simulation
WebElement menu = driver.findElement(By.xpath("//*[@id=\"sliderLocation\"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", menu);
menu.sendKeys("London");
Thread.sleep(4000);
String requiredCity = "London";
List<WebElement> menu2 = driver.findElements(By.xpath("//ul[@id='ui-id-3']/li"));
System.out.println("Total options: " + menu2.size());
for (int i = 0; i < menu2.size(); i++) {
String CurrentOption = menu2.get(i).getText();
if (CurrentOption.contains(requiredCity)) {
System.out.println("Found the city : " + CurrentOption);
menu2.get(i).click();
Thread.sleep(6000);
menu.sendKeys(Keys.RETURN);
}
}
Thread.sleep(8000);
} catch (Exception exp) {
System.out.println("exception:" + exp);
//close and quit windows
driver.close();
driver.quit();
}
//close and quit
driver.close();
driver.quit();
WebElement selectMyElement = driver.findElement((By.xpath("//div/select/option[@value='Your value']")));
selectMyElement.sendKeys("Your value");
Actions keyDown = new Actions(myLauncher.getDriver());
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN)).perform();
我正在尝试搜索此网站:http://www.jackson-stops.co.uk/
URL 中没有显示数据,所以我正在使用 chromedriver。
我的代码是:
public static void main(String[] args) {
//setup chromedriver
File file = new File("C:\Users\USER\Desktop\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.jackson-stops.co.uk/");
//begin the simulation
WebElement menu = driver.findElement(By.xpath("//*[@id=\"sliderLocation\"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", menu);
menu.sendKeys("Knightsbridge");
Thread.sleep(4000);
Select menu2 = new Select(menu);
menu2.selectByVisibleText("Knightsbridge");
Thread.sleep(4000);
} catch (Exception exp) {
System.out.println("exception:" + exp);
//close and quit windows
driver.close();
driver.quit();
}
//close and quit windows
driver.close();
driver.quit();
}
我得到的错误是:
exception:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
我如何才能 select 一个位置并按回车键,因为我尝试检查 HTML 并且选项是动态加载的,所以不可见!
您正在尝试 select 一个元素,但它不是 select 列表,它是一个 link,因此您所要做的就是单击该元素,仅此而已
首先传值
driver.findElement(By.xpath("//*[@id='sliderLocation']")).sendKeys("Knightsbridge")
一旦完成,它就会填充值,所以你需要点击其中一个选项,所以你可以直接点击这个元素(因为这个填充需要时间,你需要使用隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS))
然后写
driver.findElement(By.xpath("//a[text()='Knightsbridge, London']")).click()
或者如果你想选择由Knightsbridge
组成的元素然后写下面的代码这将选择第一个由Knightsbridge
组成的选项然后写
driver.findElement(By.xpath("//a[contains(text(),'Knightsbridge']")).click()
您不必在 selenium 代码中的任何地方使用 sleep 语句,selenium 会在点击后自动等待,直到一切正常。一个例外情况是,如果您的页面在将值放入文本框后刷新(select_list不需要),那么您需要使用隐式等待,否则甚至不需要隐式等待。
上面的代码我从Ruby转换成Java,我用来检查的原始代码来自selenium Ruby绑定,代码如下
@driver.find_element(:xpath, "//*[@id='sliderLocation']").send_keys "Knightsbridge"
@driver.manage.timeouts.implicit_wait = 10
@driver.find_element(:xpath, "//a[contains(text(),'Knightsbridge')]").click
@driver.find_element(:xpath, "//a[text()='Knightsbridge, London']").click
您可以进行以下操作:
String requiredCity = "London";
List<WebElement> menu2 = driver.findElements(By.xpath("//ul[@id='ui-id-3']/li"));
System.out.println("Total options: "+menu2.size());
for(int i=0;i<menu2.size();i++)
{
String CurrentOption = menu2.get(i).getText();
if(CurrentOption.contains(requiredCity)){
System.out.println("Found the city : "+CurrentOption);
menu2.get(i).click();
}
}
这是使用 Ranjeet 回答的完整解决方案。
File file = new File("C:\Users\USER\Desktop\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver = new ChromeDriver();
try {
driver.get("http://www.jackson-stops.co.uk/");
//begin the simulation
WebElement menu = driver.findElement(By.xpath("//*[@id=\"sliderLocation\"]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", menu);
menu.sendKeys("London");
Thread.sleep(4000);
String requiredCity = "London";
List<WebElement> menu2 = driver.findElements(By.xpath("//ul[@id='ui-id-3']/li"));
System.out.println("Total options: " + menu2.size());
for (int i = 0; i < menu2.size(); i++) {
String CurrentOption = menu2.get(i).getText();
if (CurrentOption.contains(requiredCity)) {
System.out.println("Found the city : " + CurrentOption);
menu2.get(i).click();
Thread.sleep(6000);
menu.sendKeys(Keys.RETURN);
}
}
Thread.sleep(8000);
} catch (Exception exp) {
System.out.println("exception:" + exp);
//close and quit windows
driver.close();
driver.quit();
}
//close and quit
driver.close();
driver.quit();
WebElement selectMyElement = driver.findElement((By.xpath("//div/select/option[@value='Your value']")));
selectMyElement.sendKeys("Your value");
Actions keyDown = new Actions(myLauncher.getDriver());
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN)).perform();