Home > Software engineering >  Selenium / Pressing Radio Button not possible?
Selenium / Pressing Radio Button not possible?

Time:07-08

I try to press the following radio button "All Languages"

enter image description here

from this site

https://www.tripadvisor.com/Hotel_Review-g1066456-d1083444-Reviews-or0-Sakura_Hotel_Hatagaya-Shibuya_T_Kanto.html#REVIEWS

using the following code

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
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__':
  os.environ['WDM_LOG_LEVEL'] = '0' 
  ua = UserAgent()
  userAgent = ua.random
  options = Options()
  # options.add_argument('--headless')
  options.add_argument("start-maximized")
  options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})    
  options.add_experimental_option("excludeSwitches", ["enable-automation"])
  options.add_experimental_option('excludeSwitches', ['enable-logging'])
  options.add_experimental_option('useAutomationExtension', False)
  options.add_argument('--disable-blink-features=AutomationControlled')
  options.add_argument(f'user-agent={userAgent}')   
  srv=Service(ChromeDriverManager().install())
  driver = webdriver.Chrome (service=srv, options=options)    
  waitWebDriver = WebDriverWait (driver, 30)         
  
  link = f"https://www.tripadvisor.com/Hotel_Review-g1066456-d1083444-Reviews-or0-Sakura_Hotel_Hatagaya-Shibuya_T_Kanto.html#REVIEWS" 
  driver.get (link)  
  try:
    waitWebDriver.until(EC.element_to_be_clickable( \
      (By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()       
  except TimeoutException as ex:
    print(f"{ex}")
  waitWebDriver.until(EC.element_to_be_clickable( \
    (By.XPATH, "//input[@id='LanguageFilter_0']"))).click()       

  input("Press!")
  driver.quit()

But unfortunately, i get this error message:

$ python testScrape.py
Traceback (most recent call last):
  File "C:\DEV\Fiverr\TRY\david_defranza\testScrape.py", line 36, in <module>
    waitWebDriver.until(EC.element_to_be_clickable( \
  File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Why is the button not clicked / found? (the XPath i used works fine when inspecting in the chrome browser)

CodePudding user response:

use the xpath //label[@for='LanguageFilter_0'] instead of //input[@id='LanguageFilter_0'],I tried it and it work's fine for me.

CodePudding user response:

In the website it's not

id='LanguageFilter_0

but

for="LanguageFilter_0"

If this modification did not work you can still do a look on the '' and click on the first element. :)

I hope this will help you.

  • Related