向目标输入框输入值(WebDriverWait判断是否有该输入框)

# 导包
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 浏览器驱动
driver=webdriver.Chrome()
driver.get(r"D:\\myself\1.html")
sleep(3)
# 寻找元素  点击
# 等找到元素再执行
element=WebDriverWait(driver,10)
.until(EC.visibility_of_element_located((By.NAME,"username"))) element.send_keys("fqs") # driver.find_element(By.NAME,"username").send_keys("fqs") # 退出 sleep(2) driver.quit()

解释

 

 

 

 

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>CSS1</title>
</head>
<body>
  <div id='div1'>

       <div id='div2' >
         <P>这是段落1</p>
           <form >
               <button>登录1</button><br><br>
               账号<input type="text" name="username" id="ls_username" class="pn vm"><br><br>
               密码<input type="text" name="password" id="ls_password" class="pn vm"><br><br>

           </form>


         <p>这是段落2</p>
         <a href="">baidu</a>
       </div>

       <div id='div3'>
           <p>这是段落3</p>
           <button type="button" id="loginbutton">登录</button>
       </div>
  </div>
</body>
</html>

最终效果

 

posted @ 2023-09-27 12:19  胖豆芽  阅读(19)  评论(0编辑  收藏  举报