使用带java的webdriver获取div id值

使用带java的webdriver获取div id值,java,html,css,xpath,selenium-webdriver,Java,Html,Css,Xpath,Selenium Webdriver,从下面的HTML源代码中,我想在优先级部分下获取表的id值(DataTables\u table\u 1),因为此id动态生成类似(DataTables\u table\u 1,DataTables\u table\u 2,DataTables\u table\u 2) 我得到的是输出,但不是预期的结果,因为className应用了多次,所以它选择了其他id,而不是priority div下的pick 请指导我。您可以使用cssSelector指定路径 WebElement checPri =

从下面的HTML源代码中,我想在优先级部分下获取表的id值(
DataTables\u table\u 1
),因为此id动态生成类似(
DataTables\u table\u 1,DataTables\u table\u 2,DataTables\u table\u 2

我得到的是输出,但不是预期的结果,因为className应用了多次,所以它选择了其他id,而不是priority div下的pick


请指导我。

您可以使用
cssSelector
指定路径

WebElement checPri = driver.findElement(By.cssSelector("#priority .dataTables_wrapper"));
String insideDiv = checPri.getAttribute("id");
或者查找具有类
dataTables\u包装器
和部分id
dataTables\u Table

WebElement checPri = driver.findElement(By.id("priority"));
String insideDiv = checPri.findElement(By.cssSelector("[id*='DataTables_Table'].dataTables_wrapper")).getAttribute("id");

要在优先级部分下检索表
id
值(例如
DataTables\u table\u 1
),可以使用以下代码行:

System.out.println(driver.findElement(By.xpath("//div[@id='priority']//table[@class='ohim-table  dataTable']")).getAttribute("id"));    

你能打印你的变量吗<代码>字符串insideDiv?是的,获取DataTables\u Table\u 1\u包装器没有获取表id,因此如何包含表标记
WebElement checPri = driver.findElement(By.id("priority"));
String insideDiv = checPri.findElement(By.cssSelector("[id*='DataTables_Table'].dataTables_wrapper")).getAttribute("id");
System.out.println(driver.findElement(By.xpath("//div[@id='priority']//table[@class='ohim-table  dataTable']")).getAttribute("id"));