这是代码
`<div id="test-container"><div id="text-container"><div id="text-highlight" style="width: 48px; left: 30px; opacity: 1;"></div> <div id="test-text" class="fast-fade-in" style="min-height: 77px;"><span class="test-word">we </span><span class="test-word">like </span><span class="test-word">form </span><span class="test-word">world </span><span class="test-word">plan </span><span class="test-word">at </span><span class="test-word">look </span><span class="test-word">good </span><span class="test-word">number </span><span class="test-word">few </span><span class="test-word">more </span><span class="test-word">write </span></div></div> <div id="test-bar"><input type="text" id="test-input" spellcheck="false" autocomplete="off"> <div class="bar-items"><div id="wpm-display-container" class="bar-item"><span style="">0 <small> WPM</small></span></div> <div id="timer-display-container" class="bar-item"><span style="">0:10</span></div> <button id="reset-button" aria-label="Reset Test" class="bar-item button-highlight"><svg xmlns="http://www.w3.org/2000/svg" id="refresh-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-refresh-ccw"><polyline points="1 4 1 10 7 10"></polyline> <polyline points="23 20 23 14 17 14"></polyline> <path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15"></path></svg></button></div></div></div>`
输入元素或feild就是这个
我想使用selenium访问它,并想使用sendKeys((函数,但问题是即使是find_element_by_id((也无法正常工作
这是我的代码,它可能看起来很奇怪,因为我在repl.it 中使用了它
`from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
list=[]
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://typetest.io/")
element = driver.find_element_by_id("test-input")
element.sendKeys("Hello")`
尝试显式等待:
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get("https://typetest.io/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "test-input"))).send_keys("Hello")
进口:
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 import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
list=[]
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 20)
driver.get("https://typetest.io/")
element = wait.until(EC.visibility_of_element_located((By.ID, "test-input")))
element.sendKeys("Hello")