无法单击div标签元素

时间:2017-03-08 22:24:18

标签: java selenium selenium-webdriver

我需要点击“管理”标签。 html代码如下

<div id="TransactionLinksTabContainer">
      <ul class="TransactionLinksTab listitem-cursor-pointer" id="ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList">
         <li><div>Transactions</div></li>
         <li><div>Restricted</div></li>
         <li><div>Quotes</div></li>
         <li><div>Admin</div></li>
     </ul>
</div>

我尝试了这个无效的解决方案

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*   [@id='TransactionLinksTabContainer']/ul/li[4]/div[text()='Admin']")));
element.click();

3 个答案:

答案 0 :(得分:0)

您也可以使用descendant功能来达到所需的控制,如下所示:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='TransactionLinksTabContainer']/descendant::div[text()='Admin']")));
element.click();

答案 1 :(得分:0)

我建议您使用

<div class="fixed">
    <a href="contactX.html"><img src="images/contact.png" class="img-responsive alt="icon 01" width="300px"></a>                    
</div>
<div class="fixed1">
    <a href="news.php"><img src="images/news.png" class="img-responsive alt="icon 01" width="300px"></a>
</div>
<div class="fixed2">
    <a href="success.php"><img src="images/success.png" class="img-responsive" alt="icon 01" width="300px" ></a>
</div>
<div class="fixed3">
    <a href="product.html"><img src="images/product.png" class="img-responsive" alt="icon 01" width="300px"></a>
</div>
<div class="fixed4">
    <a href="aboutus.html"><img src="images/company_profile.png" class="img-responsive" alt="icon 01" width="300px"></a>
</div>

我不认为XPath存在问题,可能是“Wait元素无法存储在Webelement中”

答案 2 :(得分:0)

尝试使用java-script executor方法单击元素以单击元素。

WebElement admin = driver.findElement(By.xpath("//ul[@id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", admin);
  


使用显式等待方法,然后尝试单击元素。

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//ul[@id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]"))));
driver.findElement(By.xpath("//ul[@id='ctl00_PostIssueContent_PostIssueTrxLinks_TransactionTabsList']/li/div[contains(text(), 'Admin')]")).click();