Selenium发送密钥(文本),从下拉列表中选择并按Enter键

时间:2017-03-18 05:33:21

标签: java selenium drop-down-menu sendkeys

我正在尝试浏览此网站:http://www.jackson-stops.co.uk/

数据未显示在网址中,因此我使用的是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"

我如何能够选择位置并按Enter键,因为我尝试检查HTML并且选项是动态加载的,因此不可见!

4 个答案:

答案 0 :(得分:1)

您正在尝试选择一个元素,但它不是选择列表,它是一个链接,所以您只需要点击该元素即可全部

首先传递值

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()

您不必在硒代码中的任何地方使用睡眠声明,硒会在点击后自动等待,直到所有内容都能正常结算。一个例外情况是,如果您的页面在将值放入文本框后得到刷新(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

答案 1 :(得分:1)

您可以执行以下操作:



 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();
	        	}
	        }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是使用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();

答案 3 :(得分:0)

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();