所以这是我试图进入
的元素<table border="0" cellpadding="0" cellspacing="0" width="930"><tbody>
<tr>
<td height="32"><span style="cursor: pointer; text-decoration:underline; font-size:14px;" onClick="commentsDisplay(9427356)"><b>Display / write / close (0) comment(s) for this video.</b></span> </td>
</tr> </tbody></table>
它没有ID,没有名称,没有任何内容......此网页中有多个表格元素...如何让它与众不同并让WebDriver
点击它?我尝试使用 partialLinkText ,但它没有用(有点预期,因为它不是属性标记)
这是我的java课程
// Find the text input element by its name
WebElement element = driver.findElement(By.id("q"));
// Enter something to search for
element.sendKeys(searchQ[rnd.nextInt(4)]);
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
//Open random Video
driver.findElement(By.className("miniature")).click();
//Open Comments box
By xpath = By.xpath("//span[contains(@onClick,'commentsDisplay')]/b");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(xpath));
myDynamicElement.click();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
答案 0 :(得分:0)
尝试以下 xpath
//span[contains(@onClick,'commentsDisplay')]/b
或 css
span[onClick^='commentsDisplay']>b
如果该元素需要一些时间来加载用途explicit
请等待:
By xpath = By.xpath("//span[contains(@onClick,'commentsDisplay')]/b");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(xpath));
myDynamicElement.click();
答案 1 :(得分:0)
尝试获取您尝试从html中单击的表标记的索引,然后尝试下面的代码
driver.findElement(By.xpath("//table[1]/tr/td[contains(@onClick,'commentsDisplay')]")).click();
如果页面中的表格是动态显示的,那么您需要Thread.sleep(some time)
一段时间(观察大约需要多少手动加载)并单击该元素。