selenium webdriver - 无法根据我使用部分文本的搜索选择任何项目

时间:2016-05-10 11:10:59

标签: selenium selenium-webdriver

enter image description here我的情况如下。

我想根据我的搜索文本选择一些技能" Data Analytics"在naukri.com

我无法记下脚本并选择列表中的任何项目。任何人都可以在这方面为我提供帮助。我已在此链接中附上了带有html代码的快照供您参考。HTML with the search page

2 个答案:

答案 0 :(得分:0)

你可以在ur xpath中使用contains,如下所示:

在搜索框中写入数据后,请尝试以下代码:

Thread.sleeep(2000);
//this will show all the elements containg the data keyword.
List<WebElement> elem = driver.findElements(By.xpath("//*[@id='sugDrp_skill']/ul/li/div[contains(text(),'Data')]"));

for(WebElement element : elem){      

    //this will hover to ur all search result elements one by one
    Actions builder = new Actions(driver);  
    builder.moveToElement(element).build().perform();

    //If u want to click based on condition use this
    //this will click on clinical data management search result        
    if( element.getText().equals("Clinical Data")){
        Actions builder1 = new Actions(driver); 
        builder1.moveToElement(element).click().build().perform();
    }

}

答案 1 :(得分:0)

试试这段代码。它应该工作正常。 (等待调试方法只使用更合适的服务员)。

 @Test
  public void selectSkill() {
    FirefoxDriver driver = new FirefoxDriver();
    driver.get("http://www.naukri.com/");
    selectSkillByIndex(1);
  }

  public void selectSkillByIndex (int index) {
    By closeButton = By.xpath("(//span[@class='close'])[1]");
    By dropdownItems = By.xpath("//li[@class='sugTouple']");
    WebElement skillInput = driver.findElement(By.xpath("//div[@id='skill']//input"));
    Actions action = new Actions(driver);
    skillInput.click();
    waitForCondition(10, ExpectedConditions.visibilityOfElementLocated(closeButton), driver);
    skillInput.sendKeys("data");
    waitForCondition(10, ExpectedConditions.visibilityOfElementLocated(dropdownItems), driver);
    waitFor(1000);
    action.click(driver.findElements(dropdownItems).get(index)).perform();
    action.click(driver.findElement(closeButton)).perform();
  }

  public void waitForCondition(int seconds, ExpectedCondition<?> condition, WebDriver driver) {
      Wait<WebDriver> wait = new WebDriverWait(driver, seconds).ignoring(org.openqa.selenium.NoSuchElementException.class);
      wait.until(condition);
  }

  public static void waitFor(int milliseconds) {
    try {
      Thread.sleep(milliseconds);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }