【问题标题】:Python Selenium Chrome Webdriver : bound method WebElement.clickPython Selenium Chrome Webdriver:绑定方法 WebElement.click
【发布时间】:2021-10-28 13:00:20
【问题描述】:

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

存款元素的 HTML:

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

存款元素的快照:

我的代码是

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")>>

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

【问题讨论】:

  • 添加您尝试点击的元素的相关 HTML。
  • 不是元素的截图,而是元素的相关HTML。

标签: python selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

点击文字为Deposit的元素你要诱导WebDriverWait,你可以使用以下Locator Strategies之一:

  • 使用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
    

【讨论】:

  • 感谢您的意见。在遵循'from selenium.webdriver.support.ui import WebDriverWait'from selenium.webdriver.support import expected_conditions as EC 之后,我尝试了这两个答案,但是在消息之后出现NameError: name 'By' is not defined 我是新手,对如何使用它感到困惑。请再帮忙
  • 你错过了导入from selenium.webdriver.common.by import By
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2021-05-27
  • 2017-07-17
  • 2012-06-10
  • 2017-08-12
相关资源
最近更新 更多