0

I'm facing an issue locating the element on screen when there are no unique identifiers like ID, text etc. As it opens URL, I need to scroll down and click on button - 'Get Started' to proceed!...

Below is my code:

global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
    
driver.get("https://My URL")
driver.implicitly_wait(10)

screen = driver.find_element(By.XPATH, '//div[@class="swiper-wrapper"]')
screen.click()  (- This step doesnt through any error, i tried using this to scroll down the screen)

element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]')
driver.execute_script("arguments[0].scrollIntoView(true);", element )
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[contains(text(),"Get Started")]'))).click()
or 
element.click()

Please help me in determining how to locate the element.

enter image description here

enter image description here

1 Answers1

0

In this case you are trying to find span which is inside #shadow-root, Selenium won't be able to click elements inside #shadow-root in easy way. Look here: How to click button inside #shadow-root (closed) using Selenium and Python

But in your case you probably don't need/want to click this specific span, becouse you have custom element ion-button, which has some click event listener attached to it.

You can check XPATH value in browser devtools. If you go to Elements tab, you can right click desired element and choose option Copy>Copy Xpath. Also make sure your element is not inside iframe tag, it could cause problem as well.

Jacek Ratajewski
  • 248
  • 1
  • 10
  • This is the XPATH, - /html/body/app-root/ion-app/ion-router-outlet/app-intro[2]/ion-content/ion-slides/div[1]/ion-slide[1]/ion-row/ion-button/span..... And im not able to locate the element !! – Pooja Kayi Apr 15 '22 at 12:11
  • Are methods throwing any error? Maybe button is under other element, tho it can't be clicked rather than located. – Jacek Ratajewski Apr 15 '22 at 13:20
  • For the above mentioned code - I am getting this error - selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=100.0.4896.88) – Pooja Kayi Apr 18 '22 at 05:53
  • Also i tried to use this: --- element = driver.find_element(By.XPATH, '//span[contains(text(),"Get Started")]').get_attribute("innerText") print (element) element.click() Error i got : GET STARTED - print value AttributeError: 'str' object has no attribute 'click' – Pooja Kayi Apr 18 '22 at 06:51
  • Please help me with this ! – Pooja Kayi Apr 19 '22 at 05:07
  • Okey so this error `selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable` it says that Element was found but is not interactable, which means your XPATH is correct, you just cannot click this element. Can you provide screenshot of website and button that you want to click? – Jacek Ratajewski Apr 19 '22 at 06:22
  • Here are the details - https://i.stack.imgur.com/yK6Of.jpg – Pooja Kayi Apr 19 '22 at 06:32
  • Ohh now i get it. You dont want to search for `span` which is inside shadow-root, what you need to find and click is `ion-button` element. – Jacek Ratajewski Apr 19 '22 at 08:24
  • I edited my answer for more details. – Jacek Ratajewski Apr 19 '22 at 08:28
  • Im using python sir. If I use this - element = driver.find_element(By.XPATH, '//ion-button[@class="md button button-block button-round button-solid ion-activatable ion-focusable hydrated"]').get_attribute("innerText") print (element) element.click() I get this alert -click()AttributeError: 'str' object has no attribute 'click' – Pooja Kayi Apr 19 '22 at 09:34
  • question is why are you using .get_attribute("innerText")? It will return `string` which won't have `.click()` method. But this code will return element: `element = driver.find_element(By.XPATH, '//ion-button[@class="md button button-block button-round button-solid ion-activatable ion-focusable hydrated"]')` which will have `.click()` method – Jacek Ratajewski Apr 19 '22 at 09:42
  • If so again i see this alert sir.....selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable (Session info: chrome=100.0.4896.88) – Pooja Kayi Apr 19 '22 at 09:47
  • if element is still not interactable, you can also click it via JavaScript, look here: https://stackoverflow.com/questions/56194094/how-to-fix-this-issue-element-not-interactable-selenium-python – Jacek Ratajewski Apr 19 '22 at 11:21