How to get div text by id with selenium

Question:

I am trying to get a book’s description text from its amazon webpage. I get the book’s image and title and price fine by using driver.find_element_by_id, but when it comes to the description which is in a div with id="iframeContent", it doesn’t work. Why? I have also tried WebDriverWait but no luck.

I use the following code:

def get_product_description(self, url):
    """Returns the product description of the Amazon URL."""
    self.driver.get(url)
    try:
        product_desc = self.driver.find_element_by_id("iframeContent")
    except:
        pass

    if product_desc is None:
        product_desc = "Not available"
    return product_desc

enter image description here

Asked By: Mike

||

Answers:

you need to use text method from selenium.

try:
    product_desc = self.driver.find_element_by_id("iframeContent").text
    print(product_desc)
except:
    pass

Update 1:

There’s a iframe involved so you need to change the focus of webdriver to iframe first

iframe = self.driver.find_element_by_id("bookDesc_iframe")
self.driver.switch_to.frame(iframe)
product_desc = self.driver.find_element_by_id("iframeContent").text
print(product_desc)

Now you can access every element which is inside bookDesc_iframe iframe, but the elements which are outside this frame you need to set the webdriver focus to default first, something like this below :

self.driver.switch_to.default_content()
Answered By: cruisepandey

Since that element is inside the iframe you have to switch to that iframe in order to access elements inside the iframe.
So first you have to locate the iframe

iframe = driver.find_element_by_id("bookDesc_iframe")

then switch to it with

driver.switch_to.frame(iframe)

Now you can access the element located by the div#iframeContent css_selector
And finally you should get out from the iframe to the default content with

driver.switch_to.default_content()

Credits to the author

Answered By: Prophet