如何获取div标签的值

时间:2017-03-03 07:20:04

标签: java selenium selenium-webdriver

如何从中获取MY_TROUBLE_TWO并点击?

我已经尝试了下面提到的代码,但它没有为我工作。

尝试代码1)

WebElement element = driver.findElement(By.xpath(".//div[7]/div/div/div/div/div/div/div[2]/div[@class='dx-item-content dx-list-item-content']"));
element.click();

尝试代码2)

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//div[7]/div/div/div/div/div/div/div[2]/div[@class='dx-item-content dx-list-item-content']"))).click();

尝试代码3)

Select select = new Select(driver.findElement(By.xpath(".//xpath_to_item")));
select.selectByVisibleText("MY_TROUBLE_TWO");

我的html代码如下:

<div class="dx-scrollview-content" onclick="void(0)">
  <div id="50d24777-593c-7626-d924-2a3db0e72e89" class="dx-item dx-list-item dx-list-item-selected dx-state-focused" role="option" aria-selected="true" style="">
    <div class="dx-item-content dx-list-item-content">MY_TROUBLE_ONE</div>
  </div>
  <div class="dx-item dx-list-item" role="option" aria-selected="false">
    <div class="dx-item-content dx-list-item-content">MY_TROUBLE_TWO</div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

请尝试以下代码获取MY_TROUBLE_TWO

的值

注意:使用absolute xpath而不是relative xpath

说明:在转到元素前等待一段时间,等待我使用explicit wait方法。使用带有text标记的div方法等待查找元素后获取元素。

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//div[contains(text(), 'MY_TROUBLE_TWO')]")))).click();