Home > Net >  How to simulate a clicking at this on/off button in python selenium?
How to simulate a clicking at this on/off button in python selenium?

Time:11-10

I am trying to write a selenium code to simulate a switch on/off action.

This is the UI of that application and its actual source code under:

enter image description here

As we can see the label::before is mapped to "on" action, and label::after is for "off".

Following is my current python code that is not able to make such click.

def turnoff_byPlanCapacity(self):
    try:
        by_Plan_Capacity     =  "//div[contains(text(), 'Modem1 Settings')]/following-sibling:div[2]/div[2]/div[1]/div[2]/div/label"
        WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, by_Plan_Capacity))).click()
    except Exception as e:
        self.logger.info(e)
        raise

Does anyone know a solution for it ? Thanks a lot

CodePudding user response:

Please use this xpath

//input[starts-with(@id,'byPlanCapacity') and @type='checkbox']//following-sibling::label

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If we have unique entry in HTMLDOM, then

def turnoff_byPlanCapacity(self):
    try:
        by_Plan_Capacity = "//input[starts-with(@id,'byPlanCa[acity') and @type='checkbox']//following-sibling::label"
        WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, by_Plan_Capacity))).click()
    except Exception as e:
        self.logger.info(e)
        raise

Note that I am using element_to_be_clickable instead of presence_of_element_located

CodePudding user response:

To simulate clicking on the On / Off button you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "label[for*='byPlanCapacity']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//label[starts-with(@for, 'byPlanCapacity')]").click()
    

The desired element is a dynamic element, so 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, "label[for*='byPlanCapacity']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'byPlanCapacity')]"))).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
    

Your effective code block will be:

def turnoff_byPlanCapacity(self):
    try:
        WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'byPlanCapacity')]"))).click()
    except Exception as e:
        self.logger.info(e)
    raise
  • Related