Home > Software design >  Selenium - can´t find select-element
Selenium - can´t find select-element

Time:11-12

I try to automate this web page: https://www.crownandcaliber.com/pages/sell-my-watch-iq#top

I am using the following code, which works fine till the select-element on the page:

import time
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent

if __name__ == '__main__':
  WAIT = 5
  link = "https://www.crownandcaliber.com/pages/sell-my-watch-iq#top"
  ua = UserAgent()
  userAgent = ua.random
  options = Options()
  # options.add_argument('--headless')
  options.add_experimental_option ('excludeSwitches', ['enable-logging'])
  options.add_argument("start-maximized")
  options.add_argument('window-size=1920x1080')                               
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-gpu')  
  options.add_argument(f'user-agent={userAgent}')   
  srv=Service(ChromeDriverManager().install()) 
  driver = webdriver.Chrome (service=srv, options=options)                 
  driver.get (link) 
  time.sleep(WAIT)

  driver.find_element(By.XPATH, "//button[@id='onetrust-accept-btn-handler']").click()
  time.sleep(WAIT)
  print(f"Try to close info window...")
  try:
    driver.find_element(By.XPATH, "//button[@title='Close']").click()
  except:
    pass
  time.sleep(1)
  driver.find_element(By.XPATH,'//select').click()

When i run this code i get the following error:

Try to close info window...
Traceback (most recent call last):
  File "C:\DEV\Fiverr\TRY\ymarket1ng\checkClocks.py", line 50, in <module>
    driver.find_element(By.XPATH,'//select').click()
  File "C:\DEV\.venv\Normal\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1153, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\DEV\.venv\Normal\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 400, in execute 
    self.error_handler.check_response(response)
  File "C:\DEV\.venv\Normal\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 236, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select"}
  (Session info: chrome=94.0.4606.71)

When I inspect the site I find the single element with the XPATH "//select"

So why is this not working when running it with Selenium?

CodePudding user response:

That element is inside the iframe. You need to switch to that iframe in order to access it.
This should work:

import time
from selenium import webdriver
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from fake_useragent import UserAgent

if __name__ == '__main__':
  WAIT = 5
  link = "https://www.crownandcaliber.com/pages/sell-my-watch-iq#top"
  ua = UserAgent()
  userAgent = ua.random
  options = Options()
  # options.add_argument('--headless')
  options.add_experimental_option ('excludeSwitches', ['enable-logging'])
  options.add_argument("start-maximized")
  options.add_argument('window-size=1920x1080')                               
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-gpu')  
  options.add_argument(f'user-agent={userAgent}')   
  srv=Service(ChromeDriverManager().install()) 
  driver = webdriver.Chrome (service=srv, options=options)                 
  driver.get (link) 
  time.sleep(WAIT)

  driver.find_element(By.XPATH, "//button[@id='onetrust-accept-btn-handler']").click()
  time.sleep(WAIT)
  print(f"Try to close info window...")
  try:
    driver.find_element(By.XPATH, "//button[@title='Close']").click()
  except:
    pass
  time.sleep(1)
  iframe = driver.find_element_by_xpath("//iframe[contains(@src,'consumer')]")
  driver.switch_to.frame(iframe)
  driver.find_element(By.XPATH,'//select').click()

UPD
Here is how you can see an iframe

  • Related