【问题标题】:Can you help me type postcode into the postcode form box on this URL你能帮我在这个 URL 上的邮政编码表格框中输入邮政编码吗
【发布时间】:2021-02-09 09:38:21
【问题描述】:

我正在尝试做一个教程并在 python 中学习 Selenium,但是我似乎无法让 Selenium 将邮政编码输入到 psotcode 字段/表单框中。=

我正在使用:

Python v3.9

Chrome v87

这是我练习的网址:

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=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@contains='Postcode Checker")))
search.send_keys("NG229NU")
search.send_keys(Keys.RETURN)

【问题讨论】:

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


    【解决方案1】:

    您的定位器搜索错误,请使用:

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

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多