0

I'm trying to automate the sign in for a website and below is the code I used.

from selenium import webdriver

chromedriver = "C:/Lujing/chromedriver.exe"
browser = webdriver.Chrome(chromedriver)
browser.get("https://community-pm.p.cloud.sabrehospitality.com/pms-web-ui/login")
browser.implicitly_wait(10)

userElem = browser.find_element_by_id("spark-input_1")
userElem.send_keys("input user name") #enter user name in the quote

passwordElem = browser.find_element_by_id("spark-input_2")
passwordElem.send_keys("input password") #enter password in the quote

signin = browser.find_element_by_class_name('login-button spark-btn spark-btn--md spark-btn--primary spark-block--lte-sm spark-margin-bottom--md spark-pull-right--gte-sm')
type(signin)
signin.click()

I've also tried to use find_element_by_xpath("//*[@id='spark-input_1']"), but I keep getting below error message.

Traceback (most recent call last):
  File "C:\Lujing\Python Scripts\PMS_report_downloads.py", line 9, in <module>
    userElem=browser.find_element_by_id("spark-input_1")
  File "C:\Users\Lujing.gao\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\Lujing.gao\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Lujing.gao\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Lujing.gao\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="spark-input_1"]"}
  (Session info: chrome=80.0.3987.163)

Here is a screenshot of the source codes in the webpage

Could anyone help me with this?

Erich
  • 1,838
  • 16
  • 20
  • The problem is that the webpage implements a shadow-DOM. Look into the following similar question. https://stackoverflow.com/questions/36141681/does-anybody-know-how-to-identify-shadow-dom-web-elements-using-selenium-webdriv – Sri Apr 16 '20 at 16:16

1 Answers1

0

The following is a working solution to enter values for the username field. You can figure out how to do the password field and the submit button. Note I had issues with geckodriver. I am not sure if it works for you, but it works with Chromedriver for me.

As I mentioned in my comment to your post, the reason you cannot find the element is because of the shadowDOM. You can use some javascript to get passed that. Note in future pages of the website, you may run into a nested shadowDOM which will require a similar solution to below, but recursive.

browser = webdriver.Chrome()
browser.get("https://community-pm.p.cloud.sabrehospitality.com/pms-web-ui/login")

shadow_root = browser.execute_script('return arguments[0].shadowRoot', browser.find_element_by_tag_name("sabre-shs-login"))

userElem = shadow_root.find_element_by_id('spark-input_1')
userElem.send_keys("input user name") #enter user name in the quote
Sri
  • 2,281
  • 2
  • 17
  • 24
  • Thank you! This worked greatly and I was able to get pass it to the next page, which as you suggested also has the shadow-root in place. I tried using the same code to find an element within the shadow root, but it couldn't find the element again. The code to find the shadow root itself worked, just the find element within the shadow root failed. Would you happen to know why? I've been looking everywhere online and couldn't find anything. – nicole9095 Apr 18 '20 at 23:33
  • The tag_name for the shadow-root on the next page is most probably not sabre-shs-login. You will need to substitute that with the correct tag – Sri Apr 18 '20 at 23:35