Question

enter image description here

Hi,

I would like to write an xpath expression to ONLY print the text for all the "class - insights type1". I don't need to print the ones that has "hide" .

Following CSS selector gives me all the tags for the Insights popup

driver.find_elements_by_css_selector("#body > div.side-module.expanded > div.content")

Can someone help with this please?

Thanks

Was it helpful?

Solution

Here's how you can do this:

//div[@class="content"]/div[@class="insights type1"]

In python call find_elements_by_xpath() and get the text of each div found:

for div in driver.find_elements_by_xpath('//div[@class="content"]/div[@class="insights type1"]'):
    print div.text

Note that if you need only one single div, use just find_element_by_xpath():

div = driver.find_element_by_xpath('//div[@class="content"]/div[@class="insights type1"]')
print div.text

Also, if these divs can be outside of content div too - use //div[@class="insights type1"].

Hope that helps.

OTHER TIPS

You can use Chrome's Dev Tools for this.

Fire up the dev tools using F12, and just right-click the element whose xpath you want.

Then use lxml to process the xml tree.

image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top