Python Selenium Chrome Webdriver:绑定方法 WebElement.click

Python Selenium Chrome Webdriver : bound method WebElement.click

我是 python 的新手。我试图通过提供用户名和密码来提取 https://live-cosmos.finq.com/trading-platform/?path=details&symbol=SILVER#login 上的数据。有用。 但是当尝试点击另一个按钮时它不起作用。 ('Deposite'或'X'关闭)

Deposit 元素的

HTML:

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

Deposit 元素的快照:

我的密码是

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("username@gmail.com")
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()

输出为

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

你能帮我解决这个问题(或其他方法)并继续从网站上抓取一些文本吗? 提前谢谢你。

单击带有文本 Deposit 的元素,您必须诱导 WebDriverWait and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary' and text()='Deposit']"))).click()
    
  • 注意:您必须添加以下导入:

     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC