Question

for position in driver.find_elements_by_xpath("//div[@class='d3-tip n']"): style = position.get_attribute('style') opacity = style[:32] if opacity == "position: absolute; opacity: 1;": tooltipmessage = driver.find_element_by_xpath('//div[contains(@style,"%s")]' % opacity) time.sleep(3) print tooltipmessage.textI have 2 div tags with the same class ("d3-tip n"). When I hover the mouse the Opacity changes to "1" in the style attribute and I want to print the text from that div tag.

I wrote the following code and for some reason it does not print anything.

NOTE - I also tried position.text and that doesn't work either.

Attached is the HTML code that shows the 2 div items with the same class and the one with the SQL query is the text I want to print. enter image description here

for position in driver.find_elements_by_xpath('//div[@class="d3-tip n"]'):
                    style = position.get_attribute('style')
                    opacity = style[:32]
                    if opacity == "position: absolute; opacity: 1;":
                       print position
Was it helpful?

Solution

You can just get the first div out of 2 found found by the class name:

element = driver.find_elements_by_xpath('//div[@class="d3-tip n"]')[0]
print element.text

Another option is to check that there are no children in the div tag:

element = driver.find_element_by_xpath('//div[@class="d3-tip n"][count(*)=0]')
print element.text

Another option is to check that there is select text inside the div text:

element = driver.find_element_by_xpath('//div[@class="d3-tip n"][contains(text(), "select")]')
print element.text
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top