0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have one more [@class="insights type 2"] on the same level in the same div and I want to print both "type 1" and "type 2" classes. Will any of the following work? ("//div[@class='content'][/div[@class='insights type1']][/div[@class='insights type2']]") OR ("[//div[@class='content']/div[@class='insights type1']] and [//div[@class='content']/div[@class='insights type2']]")
@user3587233 try this one: //div[@class="content"]/div[@class="insights type1" or @class="insights type2"]
hey, can you please help me with the above question. Thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.