我想向文本字段名称为"的网页发送一个字符串;输入字段";。事实上,我可以把单词发送到页面上,但当我运行程序时,一个新的";铬";页面打开,用于测试目的。但是,我想向已经打开的chrome页面上的字段发送一个字符串。
这是我的代码:
from selenium import webdriver
import time
url = "https://10fastfingers.com/typing-test/turkish"
options = webdriver.ChromeOptions()
options.binary_location = r"C://Program Files//Google//Chrome//Application//chrome.exe"
chrome_driver_binary = 'chromedriver.exe'
options.add_argument('headless')
driver = webdriver.Chrome(chrome_driver_binary, options=options)
driver.get(url)
driver.implicitly_wait(10)
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
运行此代码时不会发生任何事情。你能帮忙吗?你能通过在url部分放一个示例网页来运行它吗?
谢谢。
编辑:当我删除选项时,它正在工作。但运行时仍在打开一个新页面。有没有办法使用已经在后台打开的页面。
chrome_driver_binary = 'chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_binary)
driver.get('https://10fastfingers.com/typing-test/turkish')
text_area = driver.find_element_by_id('inputfield')
text_area.send_keys("Hello")
在发送密钥之前单击弹出窗口。
driver.get('https://10fastfingers.com/typing-test/turkish')
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "CybotCookiebotDialogBodyLevelButtonLevelOptinAllowallSelectionWrapper"))).click()
text_area = wait.until(EC.element_to_be_clickable((By.ID, "inputfield")))
text_area.send_keys("Hello")
进口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
我不确定你的问题是什么,但如果问题是打开了多个选项卡或窗口,那么:
您可以在窗口之间切换为:
// you can move to specific handle
chwd = driver.window_handles
print(chwd)
driver.switch_to.window(chwd[-1])
您应该先切换到正确的窗口,然后才能与该窗口上的元素进行交互
只需切换到绕过索引已经打开的窗口
如果问题是你想与已经打开的chrome交互,那么你应该遵循以下步骤:
使用调试端口启动chrome:
<path>chrome.exe" --remote-debugging-port=1559
Python:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)