Home > Software design >  Select a button with Selenium
Select a button with Selenium

Time:02-06

I have a webpage I am trying to fill in. In the middle of the page there is a button i need to click, whose info in the inspection are as follow:

<label  ng- style="">
                                <input type="radio" ng-model="paziente.consenso_informato" name="consenso_informato" ng-required="true" ng-value="1"  value="1" required="required" style=""> Si
                            </label>

So I tried this code but there is no way I can click it:

 consent_xpath = "//label[@ng-class='{\"btn-success active\": paziente.consenso_informato == 1 }']/input[@value='1']"

# Wait for the element to be visible
wait = WebDriverWait(wd, 1)
element = wait.until(EC.visibility_of_element_located((By.XPATH, consent_xpath)))
                
# Scroll down to the element and click on it
wd.execute_script("arguments[0].scrollIntoView();", element)
element.click()

Also tried this:

try:
  element = wd.find_element_by_xpath("//label[@ng-class='{'btn-success active': paziente.consenso_informato == 1 }']")
  if element.is_displayed():
                        element.click()
                        print('found')
                        break
except:
  wd.execute_script("window.scrollBy(0, 100);") 

CodePudding user response:

try this:

from selenium import webdriver
driver = webdriver.Chrome()    
driver.get("your url")
button = driver.find_element( By.CLASS_NAME, 'ng-not-empty.ng-dirty.ng-valid-parse.ng-valid.ng-valid-required.ng-touched')
button.click()

CodePudding user response:

This solved my problem:

            # Click yes on consenso informato
            overlay_locator = (By.CSS_SELECTOR, ".loading-layer")
            target_locator = (By.XPATH, "/html/body/div[2]/div/form/div[2]/div[2]/div[2]/div/div/label[1]")

            # Wait for the overlay to disappear
            wait = WebDriverWait(wd, 10)
            wait.until(EC.invisibility_of_element_located(overlay_locator))
            
            # Wait for the target element to be clickable
            target = wait.until(EC.element_to_be_clickable(target_locator))
            target.click()
  • Related