如何在python中使用selenium-webdriver找到文本框

时间:2019-04-26 19:53:23

标签: python selenium xpath css-selectors webdriverwait

这是文本框的代码:

<form>
    <input autocomplete="off" id="number" name="number" type="tel" aria-describedby="error-for-number tooltip-for-number" data-current-field="number" data-fillr-id="424069765" data-fillr="bound" style="padding: 8px 10px; font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, &quot;San Francisco&quot;, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, sans-serif;">
</form>

当我尝试通过id或名称访问它时,它不起作用:

browser.find_element_by_id('number')
browser.find_element_by_name('number')

1 个答案:

答案 0 :(得分:0)

要找到 TextBox ,您必须为element_to_be_clickable()引入 WebDriverWait ,并且可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#number[name='number'][type='tel']")))
    
  • 使用XPATH

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='number' and @name='number'][@type='tel']")))
    
  • 注意:您必须添加以下导入:

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