我写了一段用python登录网站的代码。但它需要很长时间才能实现,并且没有达到预期的效果,并给出一条消息说“DevTools Listening on”。 (send_keys 命令不起作用,请在输入框中写入“密码”和“用户名”)。
代码是:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
username="rahbar"
password="password"
print("something berfor scrap")
driver = webdriver.Chrome()
driver.get("https://plp.irbroker.com/index.do")
time.sleep(100) #waits 100 seconds
driver.maximize_window()
driver.find_element("name", "j_username").send_keys(username)
driver.find_element_by_name( name="j_password" ).send_keys(password)
print("something after scrap")
终端收到的消息是
“报废前的一些事情 DevTools 监听 ws://127.0.0.1:57315/devtools/browser/fd73344f-c857-439e-b20c-c0e76d39f389 为 CPU 创建了 TensorFlow Lite XNNPACK 委托。 尝试使用仅支持静态大小张量的委托和具有动态大小张量的图(tensor#58 是动态大小张量)”
我希望代码能够在合理的短时间内打开网页并在输入框中写入“密码”和“用户名”。 你能指导我解决这个问题吗(我是Python新手,如果我的问题很微不足道,很抱歉)
time.sleep(100)
,这可能会导致长时间的延误。WebDriverWait
替换延迟以等待元素正确加载。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
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
username = "rahbar"
password = "password"
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://plp.irbroker.com/index.do")
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "j_username"))
)
driver.find_element(By.NAME, "j_username").send_keys(username)
driver.find_element(By.NAME, "j_password").send_keys(password)
driver.find_element(By.XPATH, "//button[@type='submit']").click()
print("Login fields populated successfully.")
except Exception as e:
print(f"Error occurred: {e}")
finally:
time.sleep(10)
driver.quit()