Home > Software engineering >  How to get hidden element value in selenium?
How to get hidden element value in selenium?

Time:02-03

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[@]')[0].text 
print(sku)

My result I expected is '1052656577'.

CodePudding user response:

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[@]') 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[@]')[0].get_attribute('innerText')
    driver.quit()
    print(sku)
  • Related