[英]How to click a button using selenium - website
我想在下面的代碼中打開 http 並單擊顯示燭台的圖形按鈕。 這是因為我想執行屏幕截圖並保存標題的圖形圖像。 你可以幫幫我嗎?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH="C:\Program Files (x86)\chromedriver.exe"
driver= webdriver.Chrome(PATH)
driver.get("https://www.plus500.it/Instruments/TSLA")
try:
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable(By.ID, "ButtonLine"))
element.click()
except:
print('not found')
driver.quit()
您得到的錯誤(但在您使用 try...except 時沒有出現)是:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/pythonProject3/main.py", line 14, in <module>
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable(By.ID, "ButtonLine"))
TypeError: __init__() takes 2 positional arguments but 3 were given
原因:你忘記了括號
使固定:
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.ID, "ButtonLine")))
剛剛測試了一下,效果很好。
您可以像這樣找到元素並在.click()
之后添加:
find_element_by_xpath(xpath to element).click()
您應該編寫此代碼,如下所示
xpath = browser.find_element_by_xpath('TYPE THE XPATH')
xpath.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.