Selenium can't find the elements I'm looking for - Python

Category: python selenium (6 Views)

I'm trying to get the coordinates of a button/text on a website, this will help me automate my day to day work. The problem I encounter when using Selenium is that it can't read the whole HTML code (I think).

website="the website is ServiceNow, I will provide the url only if needed"
path="D:\chrome_driver\executable\chromedriver" #it won't work without this path
service=Service(executable_path=path)  
driver=webdriver.Chrome(service=service)
driver.get(website)
driver.maximize_window()

#Here I need to login so I will skip this part of the code as I just make use of pyautogui to enter info/press tab and enter. Users need to authenticate with 2FA.

pin=input("Insert the PIN and press ENTER to continue")    #pause in the script until the PIN is inserted
driver.refresh()
sleep(3)

Here are my attempts to scrap the info:

inputElement = driver.find_element(By.XPATH, '//div[@class="list2_cell_background"]')

inputElement = driver.find_element(By.XPATH,'//span[@class="sr-only"]')

inputElement = driver.find_element(By.XPATH,'//div[@style="background-color:orange"]')

inputElement = driver.find_element(By.NAME,'2 - High')

And the last part where I'm supposed to get the coordinates:

print(inputElement.text)
loca=inputElement.location
print(loca)

To be fair there is so much HTML code (10835 lines), my IDLE shell keeps freezing every 2 seconds if I want to look through it. I used the the following method to get the HTML and inspect it in the shell and I believe nothing is missing but for some reason it can't find the xpath or name I want.

html = driver.page_source
sleep(2)
print(html)

This is the part of the HTML I'm looking for

<td class="vt" ng-non-bindable="">
  <span class="sr-only"></span>
  <div class="list2_cell_background" style="background-color:orange"></div>
  "2 - High"
</td>

This is a photo with some info of what I'm looking for. I need to get coordinates of this location on the page

And here is the error I get:

Traceback (most recent call last):
File "C:\Users\user\Desktop\scraping.py", line 40, in <module>
inputElement = driver.find_element(By.XPATH,'//div[@class="list2_cell_background"]')
File "D:\python\python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 855, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\python\python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "D:\python\python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class="list2_cell_background"]"}
(Session info: chrome=105.0.5195.127)
Stacktrace:
Backtrace:
Ordinal0 [0x002FDF13+2219795]
Ordinal0 [0x00292841+1779777]
...
RtlGetAppContainerNamedObjectPath [0x77947B2E+238]
(No symbol) [0x00000000]

I've tried this on other websites and it works but I'm not sure what's the issue here. And sorry for the long post, I don't know how to make it more clear than this.

🔴 No definitive solution yet