0

Hello i wanted to set value for an input inside a form how can I access here the html part:

<form id="starRate" action="https://nanoclub.ir/online_festival/starRate" data-id="24">
  <input name="star" value="5" class="star star-5" id="star-5" type="radio" autocomplete="off">
</form>

and here is the python part:

    form = driver.find_element_by_id('starRate')
    #this is the form i wanted to access
    
    input_ = form.find_element_by_class_name('star star-5')
    # and this is the input wanted to set value for 5
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • If you are looking at input field like textbox sendKeys function will do the work. If you looking to select a radio button this might help. https://www.guru99.com/checkbox-and-radio-button-webdriver.html – Thrilok Dec 14 '20 at 06:23

1 Answers1

0

To click on the element to set value for 5 star you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("input.star.star-5#star-5[name='star'][value='5']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//input[@class='star star-5' and @id='star-5'][@name='star' and @value='5']").click()
    

Ideally, to click on 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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.star.star-5#star-5[name='star'][value='5']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='star star-5' and @id='star-5'][@name='star' and @value='5']"))).click()
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352