+ 1

Selenium can not find element within #shadow-root (open)

StackOverflow link for more info:https://stackoverflow.com/q/63709350/14208817 I was trying to automate the docs downloading process using selenium and python but I came across this problem when trying to click the download button.It is inside the #shadow-root (open) maybe because of that it is not found.I tried many solutions for the problem but none of them is working. CODE: ---------------------------------------------------------------------- iimport sys import time from selenium import webdriver from selenium.webdriver.common.keys import Keys def expand_shadow_element(element): shadow_root = driver.execute_script( 'return arguments[0].shadowRoot', element) return shadow_root docs = sys.argv[1:] if docs == []: docs = ['scrapy'] driver = webdriver.Chrome("/home/samip/chromedriver") driver.get("https://www.google.com/") search_box = driver.find_element_by_xpath( '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input') search_box.send_keys(f'{docs[0]} readthedocs pdf download') search_box.send_keys(Keys.ENTER) time.sleep(1) source = None driver.find_element_by_xpath('//*[@id="Rzn5id"]/div/a[2]').click() infos = driver.find_elements_by_class_name('rc') for info in infos: try: check = info.find_element_by_class_name('eFM0qc') print(check.text) if check.text == 'PDF': info.find_element_by_tag_name('h3').click() break except Exception: pass time.sleep(6) ---------------------------------------------------------------------------- Site: https://buildmedia.readthedocs.org/media/pdf/python-guide/latest/python-guide.pdf While trying to use the above user defined function as suggested in answer to similar question the output is None if the element is body and NoSuchElementException is raised if the element is pdf-viewer and other element within #shadow-root (open). I want to click the download button.

3rd Sep 2020, 3:45 PM
Samip Karki
Samip Karki - avatar
11 Respuestas
0
Yo dont put .click at end of "driver.find_element_by_tag_name(h3)" Instead dothis : Keep the inital driver.find_element but just delete the .click method. instead underneath type: ACTUALLY juss copy and overwrite yours with this search_box= driver.find_element_by_tag_name("h3) time.sleep(2) search_box.send_keys(Keys.ENTER) print("Clicked")
4th Sep 2020, 12:47 PM
LoloXploit
LoloXploit - avatar
0
from selenium.webdriver.common.keys import KEYS
4th Sep 2020, 12:48 PM
LoloXploit
LoloXploit - avatar
0
Enjoy
4th Sep 2020, 12:48 PM
LoloXploit
LoloXploit - avatar
0
4th Sep 2020, 12:53 PM
Samip Karki
Samip Karki - avatar
0
LoloXploit help me solve the real problem which is to click the download button of the webpage given in link.
4th Sep 2020, 12:55 PM
Samip Karki
Samip Karki - avatar
0
Copy this code if using firefox, just change it to webdriver.CHROME if chrommy!! ###### $ #### %% ######## $ ### url=" paste your website here " ## This Will Be where u put website addy, ############## driver = webdriver.FIREFOX() ## Now create a varabule tha is looking for ur element ### search_field = driver.find_element_by_id("") Print("Found Element") ## Now, say it was a input text field we just need to do this ### search_field.click()
4th Sep 2020, 1:07 PM
LoloXploit
LoloXploit - avatar
0
search_field.send_keys("Hey you did it!!") ## Now u need to find btn element so## submit_btn = driver.find_element_by_id("") submit_btn.click() # if we successfully are able to licate this, it will do a print statement like up top##
4th Sep 2020, 1:11 PM
LoloXploit
LoloXploit - avatar
0
print("Found it mo fo ")
4th Sep 2020, 1:11 PM
LoloXploit
LoloXploit - avatar
0
i just learned selenium, its challanging
4th Sep 2020, 1:11 PM
LoloXploit
LoloXploit - avatar
0
legit just picked it uo last night im still getting the bugs out the way
4th Sep 2020, 1:12 PM
LoloXploit
LoloXploit - avatar
0
I am sorry man but finding that download button is not that simple it is inside "shadow-dom" because of that we can't simple find with your method it needs javascript but i don't know how or anything about javascript but thanks for helping.
4th Sep 2020, 4:53 PM
Samip Karki
Samip Karki - avatar