Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium-Python: Class containing link-text

I am using Python & Selenium to scrap the content of a certain webpage. Currently, I have the following problem: There are multiple div-classes with the same name, but each div-class has different content. I only need the information for one particular div-class. In the following example, I would need the information in the first "show_result"-class since there is the "Important-Element" within the link text:

<div class="show_result">
    <a href="?submitaction=showMoreid=77" title="Go-here">
    <span class="new">Important-Element</span></a>
    Other text, links, etc within the class...
</div>

<div class="show_result">
    <a href="?submitaction=showMoreid=78" title="Go-here">
    <span class="new">Not-Important-Element</span></a>
    Other text, links, etc within the class...
</div>

<div class="show_result">
    <a href="?submitaction=showMoreid=79" title="Go-here">
    <span class="new">Not-Important-Element</span></a>
    Other text, links, etc within the class...
</div>

With the following code I can get the "Important-Element" and its link: driver.find_element_by_partial_link_text('Important-Element'). However, I also need the other information within the same div-class "show-result". How can I refer to the entire div-class that contains the Important-Element in the link text? driver.find_elements_by_class_name('show_result') does not work since I do not know in which of the div-classes the Important-Element is located.

Thanks, Finn

Edit / Update: Ups, I found the solution on my own using xpath:

driver.find_element_by_xpath("//div[contains(@class, 'show_result') and contains(., 'Important-Element')]")
like image 532
Finn_py Avatar asked Apr 22 '26 01:04

Finn_py


2 Answers

I know you've found an answer but I believe it's wrong since you would also select the other nodes because Important-Element is still in Non-Important-Element.

Maybe it works for your specific case since that's not really the text you're after. But here are a few more answers:

  1. //div[@class='show_result' and starts-with(.,'Important-Element')]

  2. //div[span[text()='Important-Element']]

  3. //div[contains(span/text(),'Important-Element') and not(contains(span/text(),'Non'))]

There are more ways to write this...

like image 76
becixb Avatar answered Apr 23 '26 15:04

becixb


Ups, i found the solution on my own via xpath:

driver.find_element_by_xpath("//div[contains(@class, 'show_result') and contains(., 'Important-Element')]")
like image 39
Finn_py Avatar answered Apr 23 '26 13:04

Finn_py