Home > Software engineering >  Python Selenium Chrome Webdriver : bound method WebElement.click
Python Selenium Chrome Webdriver : bound method WebElement.click

Time:10-29

I'm new to python. I tried to extract data on https://live-cosmos.finq.com/trading-platform/?path=details&symbol=SILVER#login by giving user name and password. It works. But when try to hit on another button it doesn't work. ('Deposite' or the 'X' to close)

HTML of the Deposit element:

<button class="btn btn-primary">Deposit</button>

Snapshot of the Deposit element:

enter image description here

My code is

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path="C:\\Chromedriver\\Chromedriver.exe")

driver.maximize_window();
driver.get('https://live-cosmos.finq.com/trading-platform/?path=details&symbol=SILVER#login')

time.sleep(5)
driver.find_element_by_xpath("/html/body/div[2]/div[4]/div[2]/div/div/div/div/div/div/form/div/div[3]/div[1]/input").send_keys("[email protected]")
driver.find_element_by_xpath("/html/body/div[2]/div[4]/div[2]/div/div/div/div/div/div/form/div/div[3]/div[2]/input").send_keys("password_here")
driver.find_element_by_xpath("/html/body/div[2]/div[4]/div[2]/div/div/div/div/div/div/form/div/div[3]/button").click()
time.sleep(20) # wait for loading
driver.find_element_by_xpath("/html/body/div[8]/div/div/div/div/div[3]/div[1]/div[4]/button").click()

output is

<bound method WebElement.click of <selenium.webdriver.remote.webelement.WebElement (session="28205c2be15699403ddfcc3f3ce2fa33", element="03ae577c-b3d4-423c-b7ed-1d9c5ea3ea1a")>>

Can you please help me to solve this (or a different approach) and go forward to scrape some text from the website. Thank you in advance.

CodePudding user response:

The click on the element with text as Deposit you have to induce WebDriverWait 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, "button.btn.btn-primary"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary' and text()='Deposit']"))).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