astonle
astonle

Reputation: 103

How to get hidden element value in selenium?

I want to get product_id from this website but this code don't work.

url = 'https://beyours.vn/products/mia-circle-mirror-white'
driver.get(url) 
driver.implicitly_wait(10)
sku = driver.find_elements_by_xpath('//div[@class="product__id hide"]')[0].text 
print(sku)

My result I expected is '1052656577'.

Upvotes: 1

Views: 1033

Answers (1)

Übermensch
Übermensch

Reputation: 328

It took me a minute but I found this post and it answers your question.

All you have to do is replace the text attribute with the get_attribute method and pass 'innerText' to it. As the post explains, you need to that method when dealing with hidden html.

Here's my code. I used find_elements(By.XPATH, '//div[@class="product__id hide"]') instead of the method you used due to the version of selenium I have (3.141.0), but the solution should still work. You check your version with print(selenium.__version__)

    url = 'https://beyours.vn/products/mia-circle-mirror-white'
    driver = webdriver.Chrome()
    driver.get(url) 
    sleep(3)
    sku = driver.find_elements(By.XPATH, '//div[@class="product__id hide"]')[0].get_attribute('innerText')
    driver.quit()
    print(sku)

Upvotes: 2

Related Questions