我正在使用Selenium(ChromeDriver
)自動執行chess site,但我無法單擊某個部分並移動它。我試過click()
和ActionChains
,但沒有任何工作。這裏是我的代碼:Selenium click trouble(Python)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
T = r"C:\Users\HP\Downloads\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
Driver = webdriver.Chrome(T, chrome_options=options)
Driver.get("https://www.chess.com/play/computer")
Driver.find_element_by_xpath('//*[@id="boardMessage"]/a').click()
Piece = WebDriverWait(Driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="chessboard_boardarea"]/img[22]')))
Piece.click()
當我運行腳本沒有任何反應,但白色棋子應該以黃色突出顯示。有人可以解釋爲什麼.click()
或ActionChains
不工作?我怎樣才能使它工作?
P.S.如果解決方案需要JavaScript幫助,請將其寫得更詳細,因爲我根本不瞭解JavaScript。
你的代碼對我來說看起來不錯。你可以嘗試在點擊之前添加一個小的延遲嗎? '進口時間; time.sleep(1)'在最後一行之前 – Amit
@不允許我在最後一行之前加上'time'並且它工作。 –