正确选择一个web元素通过python和selenium的xpath

正确选择一个web元素通过python和selenium的xpath

问题描述:

我正在试图在这种结构的网络中进行刮取。正确选择一个web元素通过python和selenium的xpath

<div> 
    <div class = “class1” > 
    <div class = “class2” > 
    <div class = “class3” > 
    <div style = “clear: both; ” > 
</div> 
<div> 
    <div class = “class1” > 
    <div class = “class2” > 
    <div class = “class3” > 
    <div style = “clear: both; ” > 
</div>  
<div> 
    <div class = “class1” > 
    <div class = “class2” > 
    <div class = “class3” > 
    <div style = “clear: both; ” > 
</div> 
在每个部分

有diferent信息。我想搜索class1中的一个特定单词,如果这个单词存在那么我打印这些信息。此后我有问题。在我想获得本节3级的信息之后。例如,如果我的第一部分的class1有“这个词”,那么我想在本节中获得class3的信息。

我的代码是这样的:

cs1 = driver.find_elements_by_class_name("class1") 
for i in cs1: 
    information = i.text 
    if "this word" in information: 
     print(information) 
     infclass3 = i.find_element_by_xpath('//following-sibling::div[@class = "class3"]') 
     print(infclass3.text) 

问题是这样的:我与“这个词” Class1的信息,但关于这一部分,我将不CLASS3的信息。每次总是在第一部分打印class3。例如,如果“这个词”是在第二和第三部分我得到的是这样的:

information of class1 - Section 2 
information of class3 - Section 1 
information of class1 - Section 3 
information of class3 - Section 1 

然后在该行的信息1和3是正确的。但在第2行和第4行不是,1.因为是重复2.因为在第1行中不是“这个单词”

感谢您的帮助。

我希望你有一个愉快的一天:)

+0

为什么循环,并没有得到元素(一个或多个)需要直接:'driver.find_element(S)_by_xpath('// DIV [@类= “1类” 和包含(文本() ,“this word”)] /../ div [@ class =“class3”]')' –

+0

我不知道我在做什么错,但是当我运行你的代码时 我什么也没有得到 – Alejo

+0

也许你需要发布更多你的HTML(至少如何用“这个词”部分看起来像) –

谢谢大家的帮助

最后,我以这种方式得到它:

infclass3 = i.find_element_by_xpath('following-sibling::*[2]') 

我得到Class1的元素,后来与'following-sibling::*[2]'我找到兄弟姐妹,并选择一个位于对应于CLA​​SS3 2的位置。

感谢您的关注

与您的代码的问题是,你正在试图获得来自class1元素的背景下class3元素,这意味着它只会寻找的孩子在class1元素目前在脑海分配给i ...这个对于你想要的class3元素的选择是:

infclass3 = i.find_element_by_xpath('../div[@class="class3"]') 
+0

谢谢你。你对自己的观察是正确的,但我尝试了你的解决方案,但它没有奏效。我用'follow-sibling :: * [2]'得到了它 – Alejo