我試圖用硒填充數獨,但每次都提出一個不可互動的元素
相關代碼:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://sudokutable.com/')
driver.find_element_by_xpath('//*[@id="body_wrapper"]/main/section[2]/div[1]/div[2]/div[1]').send_keys('1')
我也嘗試過,但結果相同:
sleep(1)
num = driver.find_element_by_xpath('//*[@id="body_wrapper"]/main/section[2]/div[1]/div[2]/div[1]')
num.click()
num.send_keys('1')
如果有人知道任何發送密鑰的方法,我非常感謝
uj5u.com熱心網友回復:
首先,我們需要關閉 Cookie 彈出視窗。
然后我們需要找到空單元格并嘗試填充單元格。空單元格可以通過標簽中的class屬性來識別svg。
DOM when the cell is already filled
<div class="game-grid__cell" data-group="1" data-row="0" data-column="4">
<svg class="default" viewBox="0 0 72 72">
DOM when the cell is empty
<div class="game-grid__cell" data-group="0" data-row="0" data-column="1">
<svg class="" viewBox="0 0 72 72">
# Imports required:
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://sudokutable.com/")
actions =ActionChains(driver)
wait = WebDriverWait(driver,30)
# Close Cookie pop-up
wait.until(EC.element_to_be_clickable((By.ID,"agree_cookies"))).click()
# Find empty cells and use indexing to fill them up
cell = driver.find_elements_by_xpath("//div[@class='game-grid__group'][1]/div/*[name()='svg' and @class='']")
actions.move_to_element(cell[0]).click().send_keys("1").perform()
uj5u.com熱心網友回復:
試試這個而不是num.click():
driver.execute_script("arguments[0].click();", num)
要發送密鑰,請使用:
driver.find_element_by_tag_name('body').send_keys('1')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/345986.html
上一篇:硒網路驅動程式|driver().switchTo().defaultContent()方法不會將控制元件從多個子視窗切換回父視窗
