How to grab the text while using selenium

Viewed 35

I'd like to know how I can grab the adress of this site with selenium:

page

I tried to store it within

address = driver.find_element_by_css_selector('main-content-cell.p-bottom').text

print(address)

But if I print the address nothing gets printed in the console. I think its because there are multiple elements with the classes "main-content-cell.p-bottom". What kind of locator strategy do I need to use?

I also tried

address = driver.find_element_by_css_selector('main-content-cell.p-bottom').get_attribute('innerText')
print(address)

and

address = driver.find_element_by_css_selector('main-content-cell.p-bottom').get_attribute('')
print(address)
2 Answers

You can use here (//div[@class='main-content-cell p-bottom'])[last()] or (//div[@class='main-content-cell p-bottom'])[2] xpaths
So address = driver.find_element_by_xpath('(//div[@class='main-content-cell p-bottom'])[last()]').text should work

Here is valid css:

address = driver.find_element_by_css_selector('.main-content-cell.p-bottom:nth-child(2)').text

or

address = driver.find_element_by_css_selector('.main-content-cell.p-bottom:last-child').text
Related