Home > OS >  Clicking a button by selenium
Clicking a button by selenium

Time:02-12

I'm trying to click that button, I've tried several ways and I couldn't.

<div  data-id="rota_grupo">
   <i >
   </i>
</div>

CodePudding user response:

You can add your chromedriver path to "DRIVER_PATH" and it should work.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

CHROMEDRIVER_PATH = Service("DRIVER_PATH")

driver = webdriver.Chrome(service=CHROMEDRIVER_PATH)

button = driver.find_element(By.CLASS_NAME, "fa-users")
button.click()

CodePudding user response:

To locate the clickable 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, "div.item[data-id='rota_grupo'] > i.fal.fa-users"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='item' and @data-id='rota_grupo']/i[@class='fal fa-users']"))).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
    
  • Related