Search code examples
pythonselenium-webdrivergetelementbyid

How to get div text by id with selenium


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


Solution

  • 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()