我是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("[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()
輸出是
<bound method WebElement.click of <selenium.webdriver.remote.webelement.WebElement (session="28205c2be15699403ddfcc3f3ce2fa33", element="03ae577c-b3d4-423c-b7ed-1d9c5ea3ea1a")>>
你能幫我解決這個問題(或不同的方法)并繼續從網站上抓取一些文本。先感謝您。
uj5u.com熱心網友回復:
單擊帶有文本作為存款的元素,您必須引發WebDriverWait,您可以使用以下任一定位器策略:
使用
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/340211.html
標籤:Python 硒 硒网络驱动程序 路径 网络驱动程序等待
上一篇:如何按部門列舉行?
