在我的代码中,通过 ID 搜索元素时,它会点击其他元素。如何找到确切的元素并需要单击相同的元素。我正在使用 Selenium WebDriver 与 java 。
Log.info("Clicking on Overview and Evolution PR link");
driver.findElement(By.id("ext-pr-backlog-evolution")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ext-pr-backlog-evolution")));
我想点击概述& Evolution 标签但点击平均分配时间标签
整个HTML如下:
<li>
<a id="ext-pr" class="submenu ext-pr" name="ext-pr" href="https://10.4.16.159/reports/">Problem Reports (PR)</a>
<ul>
<li>
<a id="ext-pr-backlog-age" class=" ext-pr-backlog-age" name="ext-pr-backlog-age" href="https://10.4.16.159/extranet_prbacklogage/reports/type/default/">Age</a>
</li>
<li>
<a id="ext-timetoassign-prs" class=" ext-timetoassign-prs" name="ext-timetoassign-prs" href="https://10.4.16.159/extranet_timetoassignprs/reports/type/default/">Average Time To Assign</a>
</li>
<li>
<a id="ext-timetodeliver-prs" class=" ext-timetodeliver-prs" name="ext-timetodeliver-prs" href="https://10.4.16.159/extranet_timetodeliverprs/reports/type/default/">Average Time To Deliver</a>
</li>
<li>
<a id="ext-timetoresolve-prs" class=" ext-timetoresolve-prs" name="ext-timetoresolve-prs" href="https://10.4.16.159/extranet_timetoresolveprs/reports/type/default/">Average Time To Resolve</a>
</li>
<li>
<a id="ext-new-prs" class=" ext-new-prs" name="ext-new-prs" href="https://10.4.16.159/extranet_newprs/reports/type/default/">New</a>
</li>
<li>
<a id="ext-open-prs" class=" ext-open-prs" name="ext-open-prs" href="https://10.4.16.159/extranet_openprs/reports/">Open</a>
</li>
<li>
<a id="ext-pr-backlog-evolution" class=" ext-pr-backlog-evolution" name="ext-pr-backlog-evolution" href="https://10.4.16.159/extranet_prbacklogevolutiontendency/reports/type/default/">Overview & Evolution</a>
</li>
<li>
<a id="ext-pr-domain" class=" ext-pr-domain" name="ext-pr-domain" href="https://10.4.16.159/extranet_prperdomain/reports/">Per Prod/SW Pack/RDT Group</a>
</li>
<li>
<a id="ext-pr-tendency" class=" ext-pr-tendency" name="ext-pr-tendency" href="https://10.4.16.159/extranet_prtendency/reports/type/default/">Progression</a>
</li>
<li>
<a id="ext-rework-prs" class=" ext-rework-prs" name="ext-rework-prs" href="https://10.4.16.159/extranet_reworkprs/reports/type/default/">Rework</a>
</li>
<li>
<a id="ext-pr-timetosolveprs" class=" ext-pr-timetosolveprs" name="ext-pr-timetosolveprs" href="https://10.4.16.159/extranet_timetosolveprs/reports/type/default/">Time To Solve</a>
</li>
</ul>
</li>
请找到解决方案。如何完全点击元素..
答案 0 :(得分:3)
在这个阶段,我无法复制任何东西。如果网站公开可见,请提供链接。
您现在可以尝试更改定位器并单击方法。每次应用其中一个定位器和一个点击方法,你应该至少有7 * 3 = 21次尝试。
基本代码:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ext-pr-backlog-evolution")));
driver.findElement(By.id("ext-pr-backlog-evolution")).click();
不同的定位器:
By.id("ext-pr-backlog-evolution") // current, not working
By.cssSelector("#ext-pr-backlog-evolution")
By.cssSelector(".ext-pr-backlog-evolution")
By.cssSelector("href[*='extranet_prbacklogevolutiontendency/reports']")
By.xpath(".//a[@id='ext-pr-backlog-evolution']")
By.cssSelector(".//a[contains(@class, 'ext-pr-backlog-evolution')]")
By.cssSelector(".//a[contains(@href, 'extranet_prbacklogevolutiontendency/reports')]")
不同的点击方式:
正常点击
driver.findElement(one_of_the_locators_above).click();`
操作点击
new Actions(driver).click(driver.findElement(one_of_the_locators_above)).perform();
JavaScript点击
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(one_of_the_locators_above));
答案 1 :(得分:0)
由于id
是唯一的,您只需要使用:
$('#ext-pr-backlog-evolution').click(function() {
// Your code here
});