【问题标题】:How to disable autocomplete to send text to username field with selenium chrome python如何禁用自动完成以使用 selenium chrome python 将文本发送到用户名字段
【发布时间】:2021-04-10 13:28:50
【问题描述】:

这是我的 python/selenium 代码

username = driver.find_element_by_xpath('//*[@id="username"]')
password = driver.find_element_by_xpath('//*[@id="password"]')
enter = driver.find_element_by_xpath('//*[@id="loginbox"]/input[3]')

passfile = open(alltools.get_letter() + "path\pass.txt", "r")
line = passfile.readlines()

username.send_keys(line[0])
password.send_keys(line[1])
enter.click()

我要填写的表格是

<form style="align: center;" id="loginForm" method="post" action="j_security_check" autocomplete="off">
      <div class="identity-box" id="loginbox">
        <div class="alert-error" style="display: none">Invalid username or password</div>
        <label class="label" for="username">Username</label>
        <input type="text" class="text-input" size="30" name="j_username" id="username">
        <label class="label" for="password">Password</label>
        <input type="password" class="text-input" size="30" name="j_password" id="password">
        <input type="submit" class="button" value="Sign In">
      </div>
    </form>

我知道问题出在autocomplete="off"。如何关闭它?

【问题讨论】:

标签: python selenium xpath autocomplete css-selectors


【解决方案1】:

自动完成属性

一般autocomplete 属性用于关闭输入字段的自动完成。举个例子:

<input type="text" autocomplete="off">

autocomplete 也可以关闭整个表单的自动完成功能:

<form autocomplete="off">

这个用例

根据您共享的 HTML, 已在 &lt;form&gt; 级别关闭:

<form style="align: center;" id="loginForm" method="post" action="j_security_check" autocomplete="off">

你应该很高兴。

要将字符序列发送到用户名字段,您可以使用以下任一Locator Strategies

  • 使用css_selector

    driver.find_element_by_css_selector("label[for='username']").send_keys("thepepp")
    
  • 使用xpath:

    driver.find_element_by_xpath("//label[@for='username']").send_keys("thepepp")
    

理想情况下,要将字符序列发送到您需要为element_to_be_clickable() 诱导WebDriverWait 的元素,您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='username']"))).send_keys("thepepp")
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='username']"))).send_keys("thepepp")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

  • 但它不起作用,它可能是在 python 尝试填充表单后显示在链接中的 action="j_security_check"。
  • @thepepp 查看更新的答案并让我知道状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2021-03-24
  • 2021-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
相关资源
最近更新 更多