-2

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

starball
  • 20,030
  • 7
  • 43
  • 238
Mike
  • 369
  • 5
  • 21

2 Answers2

1

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

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

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()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Tried that, didn't work. The problem is it doesnt find the id iframeContent, let alone its text. – Mike May 07 '21 at 10:23
  • Updated the answer, you need to use switch_to.frame to switch to iframe in order to proceed further otherwise you will get noSuchElement exception – cruisepandey May 07 '21 at 10:27