Home > Enterprise >  How to click button with Selenium
How to click button with Selenium

Time:11-27

I tried with XPath but selenium can't click this image/button.

enter image description here

from undetected_chromedriver.v2 import Chrome

def test():

    driver = Chrome()
        
    driver.get('https://bandit.camp')
    WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[1]/div/main/div/div/div/div/div[5]/div/div[2]/div/div[3]/div"))).click()
if __name__ == "__main__":

    test()

CodePudding user response:

Try the below one, I checked it, and it is working fine, while clicking on the link it is opening a separate window for login.

free_case = driver.find_element(By.XPATH, ".//p[contains(text(),'Open your free')]")

driver.execute_script("arguments[0].scrollIntoView(true)", free_case)
time.sleep(1)
driver.execute_script("arguments[0].click();", free_case)

CodePudding user response:

First you need to wait for presence of that element.
Then you need to scroll the page down to make that element visible since initially this element is out of the visible screen so you can't click it.
Now you can click it and it works.
The code below is working:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://bandit.camp/"
driver.get(url)

element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(.,'daily case')][contains(@class,'v-responsive__content')]")))
element.location_once_scrolled_into_view
time.sleep(0.3)
element.click()
  • Related