【问题标题】:Please help to enter text into form box with selenium请帮助用硒在表单框中输入文本
【发布时间】:2021-01-18 10:16:49
【问题描述】:

我正在尝试编写教程并在 python 中学习 Selenium,但是我似乎无法让 Selenium 使用 find_element_by_class 或 find_element_by_XPATH 输入邮政编码。

我正在使用:

Python v3.9

Chrome v87

这是我正在练习的 URL(您可能需要在购物篮中添加一个测试项目才能看到按钮或运行脚本):

https://www.currys.co.uk/app/checkout

这是我当前的点击代码:

# Selenium Tutorial #1

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.common.action_chains import ActionChains

import time

# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")

# Open webpage
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")

# Click "Accept All Cookies" or ignore if no pop up
try:
    element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler"))
    )
    element.click()
except Exception:
    pass

# Wait 3 seconds
driver.implicitly_wait(3)

# Click "Add to Basket" or refresh page if out of stock
try:
    element = WebDriverWait(driver, 1).until(
    EC.presence_of_element_located((By.XPATH, "email-desktop"))
    )
    time.sleep(5)
    browser.refresh()
except:
    button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")
    driver.execute_script("arguments[0].click();", button)

# Wait 3 seconds
driver.implicitly_wait(3)

# Click "Continue to Basket"
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.sc-fzpjYC.gJohPa")
driver.execute_script("arguments[0].click();", button)

# Wait 3 seconds
driver.implicitly_wait(3)

# Click "Go to checkout"
button = driver.find_element_by_xpath("//button[contains(@data-component, 'Button')][contains(@type, 'button')]")
driver.execute_script("arguments[0].click();", button)

# Type in postcode
search = driver.find_element_by_xpath("//input[contains(@aria-invalid), 'False')][contains@aria-label, 'Postcode Checker')]")
search.send_keys(NG229NU)
search.send_keys(Keys.RETURN)

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    要识别input 元素,请使用locator 之一。

    XPATH:

    //input[@aria-label='Postcode Checker' and @type='search']
    

    CSS 选择器:

    input[aria-label='Postcode Checker'][type='search']
    

    为避免同步问题,我建议使用WebDriverWait() 并等待element_to_be_clickable() 并在输入 框中输入文本。

    search=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@aria-label='Postcode Checker' and @type='search']")))
    search.send_keys("NG229NU")
    search.send_keys(Keys.RETURN)
    

    【讨论】:

      【解决方案2】:

      您的 XPath 无效(第一个谓词中有额外的括号,而第二个谓词中没有左括号)。尝试替换

      "//input[contains(@aria-invalid), 'False')][contains@aria-label, 'Postcode Checker')]"
      

      "//input[contains(@aria-invalid, 'False')][contains(@aria-label, 'Postcode Checker')]"
      

      【讨论】:

      • 奇怪,好像和上面那行一模一样,在sublime 3中看起来是正确的?当我在上面发布此代码时出现错误:/
      • @StephenBrown 建议的 XPath 出现什么错误?
      • 第 56 行 search = driver.find_element_by_xpath("//input[contains(@aria-invalid, 'False')][contains(@aria-label, 'Postcode Checker')]) ^ SyntaxError : 扫描字符串时 EOL
      • @StephenBrown 哦,是的。错过了结束双引号
      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      相关资源
      最近更新 更多