1

I am trying to click on a button which looks like is present inside shadow root. Below is the image where I am trying to click:

enter image description here

When I trying to click the element above it, getting an error that element is not interactable.

Search any song on amazon prime music for doing it yourself.

Approach 1

song_result = driver.find_element(By.XPATH,"/html/body/div/music-app/div[4]/div/div/div/music-container/music-container[2]/div/music-shoveler")
song = song_result.find_element(By.TAG_NAME,"music-horizontal-item")
a = song.find_elements(By.TAG_NAME,"music-button")
a[1].click()

Approach 2

driver.find_element(By.XPATH,"/html/body/div/music-app/div[4]/div/div/div/music-container/music-container[2]/div/music-shoveler/music-horizontal-item[1]/music-button[2]//button").click()
        

Actually the order is like this:

<music-horizontal-item>
    <music-button>
        #shadowRoot
        <button>
    <music-button>
        #shadowRoot
        <button>This button I need to click

So I used this as per your suggestion

song_result = driver.find_element(By.XPATH,"/html/body/div/music-app/div[4]/div/div/div/music-container/music-container[2]/div/music-shoveler")
song = song_result.find_element(By.TAG_NAME,"music-horizontal-item")
a = song.find_elements(By.TAG_NAME,"music-button")
song_root = driver.execute_script("return arguments[0].shadowRoot",a[1])
song_root.find_element(By.TAG_NAME,"button").click()a

Still getting this error:

Message: element not interactable
EXODIA
  • 908
  • 3
  • 10
  • 28

1 Answers1

0

first you must select shadow dom as element with js then search inside it with selenium find function:

method1:

song = song_result.find_element(By.TAG_NAME,"music-button")
song_root = expand_element(song)
a = song_root.find_elements(By.TAG_NAME,"button")
a[1].click()

then write expand_element function to execute js script on element and get shadowRoot (more)

def expand_element(element):
    return driver.execute_script("return arguments[0].shadowRoot",element)

method2 (direct):

song_root = driver.execute_script("return document.querySelector('music-button').shadowRoot")
a = song_root.find_elements(By.TAG_NAME,"button")
a[1].click()

i can't see parent elements, but what was said was "the structure of finding" in shadow dom

music_h_i = driver.find_element_by_xpath("//music-horizontal-item")
music_h_i_expanded = expand_element(music_h_i)
music_button = music_h_i_expanded.find_elements(By.TAG_NAME,"music-button")
music_button_expanded = expand_element(music_button)
music_button.click() # or music_button_expanded.click()

don't need use xpath from /html/body/, xpath has very feature for search

PersianMan
  • 924
  • 1
  • 12
  • 29