为什么我不能在网站的 table 上使用 FindElementByXPath?
Why can't I use FindElementByXPath on this table from a website?
我目前正在使用 selenium 和 chromedriver 在 vba 上进行网络抓取。
我登录了一个网站(不幸的是,由于需要登录,该网站无法访问,但它是 https://monitoring.csisolar.com/login),在主页中,有一个通用的 table一堆信息。
我使用“检查元素”并确定了 table 对象,如图所示:
print of the found element, the table is highlighted
当我 select“复制 xpath”时,我得到这个:“//*[@id="mCSB_67_container"]/table"
自然,我这样设置命令:
Set tabela = driver.FindElementByXPath("//*[@id="mCSB_67_container"]/table") 'Seleciona a tabela pelo java
If tabela Is Nothing Then
MsgBox "Não encontrado"
Else
tabela.AsTable().ToExcel (ActiveSheet.Cells(1, 1)) 'Cola a tabela na planilha (aba) onde está o botão
End If
但是excell上没有打印任何东西。
我在其他类似的网站(solarweb.com)做了同样的事情并且进展顺利,但是 table 对象有一个 id 和 class 定义,而上面的那个没有命名的属性(如印刷品所示)
我知道这个问题不是很清楚,但是如果有什么可以帮助您理解的,请在下面评论,我很乐意编辑我的问题
幕后的一切都在创建动态 ID。您的目标是找到不会改变的选择器。然后使用它们向下导航 DOM,直到到达 table。在您的小摘录中,我看不到任何尖叫的唯一 ID 永远不会改变。如果你只有一个 table 可见,你可以这样找到它:
driver.findElement(By.cssSelector("table"));
mCSB_67_container
似乎是动态创建的 id
值,即这不是固定的 id
值。
我不确定(需要使用开发工具查看实际页面),但您可以尝试改用此 XPath:
Set tabela = driver.FindElementByXPath("//div[contains(@class,'mCustomScrollBox']//table")
有时很难定位元素,
如果它是静态的 table 并且动态更新值页面。最好右键单击并复制 chrome 给出的 xpath。有效地工作。而不是编写循环逻辑。
enter image description here
在这个问题上尝试的内容不起作用,因为 table 元素是一个 动态对象 ,所以它是“id”和“class " 属性不是常量,不能引用。
所以,感谢@Prophet 和@David M,我发现我应该寻找一个静态引用,而不是网站界面的一部分,它不是动态的,然后用它来引用 table.
我使用的元素是包含 ID 为“plantList”的元素的分区。这是我如何找到此元素的屏幕截图:
In red, an arrow pointing to the table element, and in green, an arrow pointing to the statc element that I used to reference
从现在开始,我只是通过所有其他元素,直到到达 table。最后一行是这样的:
Set canadian = driver.FindElementByXPath("//*[@id=""plantList""]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/table") 'Seleciona a tabela pelo java
If canadian Is Nothing Then
MsgBox "Não Encontrado"
Else
canadian.AsTable().ToExcel (ActiveSheet.Cells(1, 1)) 'Cola a tabela na planilha (aba) onde está o botão
End If
我目前正在使用 selenium 和 chromedriver 在 vba 上进行网络抓取。
我登录了一个网站(不幸的是,由于需要登录,该网站无法访问,但它是 https://monitoring.csisolar.com/login),在主页中,有一个通用的 table一堆信息。
我使用“检查元素”并确定了 table 对象,如图所示:
print of the found element, the table is highlighted
当我 select“复制 xpath”时,我得到这个:“//*[@id="mCSB_67_container"]/table"
自然,我这样设置命令:
Set tabela = driver.FindElementByXPath("//*[@id="mCSB_67_container"]/table") 'Seleciona a tabela pelo java
If tabela Is Nothing Then
MsgBox "Não encontrado"
Else
tabela.AsTable().ToExcel (ActiveSheet.Cells(1, 1)) 'Cola a tabela na planilha (aba) onde está o botão
End If
但是excell上没有打印任何东西。
我在其他类似的网站(solarweb.com)做了同样的事情并且进展顺利,但是 table 对象有一个 id 和 class 定义,而上面的那个没有命名的属性(如印刷品所示)
我知道这个问题不是很清楚,但是如果有什么可以帮助您理解的,请在下面评论,我很乐意编辑我的问题
幕后的一切都在创建动态 ID。您的目标是找到不会改变的选择器。然后使用它们向下导航 DOM,直到到达 table。在您的小摘录中,我看不到任何尖叫的唯一 ID 永远不会改变。如果你只有一个 table 可见,你可以这样找到它:
driver.findElement(By.cssSelector("table"));
mCSB_67_container
似乎是动态创建的 id
值,即这不是固定的 id
值。
我不确定(需要使用开发工具查看实际页面),但您可以尝试改用此 XPath:
Set tabela = driver.FindElementByXPath("//div[contains(@class,'mCustomScrollBox']//table")
有时很难定位元素, 如果它是静态的 table 并且动态更新值页面。最好右键单击并复制 chrome 给出的 xpath。有效地工作。而不是编写循环逻辑。
enter image description here
在这个问题上尝试的内容不起作用,因为 table 元素是一个 动态对象 ,所以它是“id”和“class " 属性不是常量,不能引用。 所以,感谢@Prophet 和@David M,我发现我应该寻找一个静态引用,而不是网站界面的一部分,它不是动态的,然后用它来引用 table.
我使用的元素是包含 ID 为“plantList”的元素的分区。这是我如何找到此元素的屏幕截图:
In red, an arrow pointing to the table element, and in green, an arrow pointing to the statc element that I used to reference
从现在开始,我只是通过所有其他元素,直到到达 table。最后一行是这样的:
Set canadian = driver.FindElementByXPath("//*[@id=""plantList""]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/table") 'Seleciona a tabela pelo java
If canadian Is Nothing Then
MsgBox "Não Encontrado"
Else
canadian.AsTable().ToExcel (ActiveSheet.Cells(1, 1)) 'Cola a tabela na planilha (aba) onde está o botão
End If