0

I'd like to locate the search text area from: https://1xbet.whoscored.com/

I used the following code in colab

from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize the webdriver
service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
browser = webdriver.Chrome(service=service, options=options)
# Navigate to a webpage
browser.get('https://1xbet.whoscored.com/')

element = browser.find_element(By.XPATH,'//*[@id="search-box"]')

element.clear()
element.send_keys('Manchester united')

# Close the driver
browser.quit()

However, it raises the following error

    ---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-130-cd823d469a61> in <cell line: 13>()
     11 browser.get('https://1xbet.whoscored.com/')
     12 
---> 13 element = browser.find_element(By.XPATH,'//*[@id="search-box"]')
     14 element.clear()
     15 element.send_keys('Manchester united')

2 frames
/usr/local/lib/python3.10/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    243                 alert_text = value["alert"].get("text")
    244             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 245         raise exception_class(message, screen, stacktrace)

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="search-box"]"}
  (Session info: headless chrome=112.0.5615.49); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

Any suggestions?!

Ahmed Hamed
  • 25
  • 1
  • 6

1 Answers1

0

The desired element is a dynamic element. So to send a character sequence within the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get("https://1xbet.whoscored.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search-box[value='Search']"))).send_keys("Manchester united")
    
  • Using XPATH:

    driver.get("https://1xbet.whoscored.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='search-box' and @value='Search']"))).send_keys("Manchester united")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser snapshot:

search


References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your comment. Unfortunately, it does not work. resulting in Timeout exception – Ahmed Hamed Jun 24 '23 at 14:16
  • @AhmedHamed That wasn't a comment either but a well researched answer which works perfecto on my Win10 system. – undetected Selenium Jun 24 '23 at 14:19
  • whatever it is a comment or well-researched answer, it does not work over colab /usr/local/lib/python3.10/dist-packages/selenium/webdriver/support/wait.py in until(self, method, message) 93 if time.monotonic() > end_time: 94 break ---> 95 raise TimeoutException(message, screen, stacktrace) 96 97 def until_not(self, method, message: str = ""): TimeoutException: Message: – Ahmed Hamed Jun 24 '23 at 14:28