如何点击li标签中的选项

时间:2016-01-23 12:23:01

标签: selenium-webdriver

我正在尝试做以下事情:

转到yelp.com 在查找

中的下拉框中选择“餐馆”

我无法点击"餐厅"因为xpath无法定位元素。

2 个答案:

答案 0 :(得分:0)

我用selenium-webdriver,junit检查过。下面的代码对我来说很好。请为您检查此做出决定。

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    baseUrl = "http://www.yelp.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testYelpRest() throws Exception {
    driver.get(baseUrl + "");
    driver.findElement(By.id("find_desc")).clear();
    driver.findElement(By.id("find_desc")).sendKeys("Restaurants");
    driver.findElement(By.id("find_desc")).sendKeys(Keys.DOWN);
    driver.findElement(By.id("find_desc")).sendKeys(Keys.ENTER);
    for (int second = 0;; second++) {
        if (second >= 60) fail("timeout");
        try { if (isElementPresent(By.xpath("//span[@class='pagination-results-window']"))) break; } catch (Exception e) {}
        Thread.sleep(1000);
    }
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

答案 1 :(得分:0)

你可以尝试下面的代码,我相信它会起作用.. :)

 public class Yelp_dropdown {

    public static void main(String[] args) {

        WebDriver driver=new FirefoxDriver();

        driver.get("http://www.yelp.com/");

        WebDriverWait w= new WebDriverWait(driver,5);

        driver.findElement(By.xpath(".//*[@id='find_desc']")).click();

        w.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@class='suggestion-detail suggestion-name' and contains (text(), 'Restaurants')]"))).click();