【问题标题】:How to click on Continue Checkout gif element on webpage using Selenium and Python?如何使用 Selenium 和 Python 在网页上单击 Continue Checkout gif 元素?
【发布时间】:2021-01-19 13:09:45
【问题描述】:

我正在尝试做一个教程并在 python 中学习 Selenium,但是我似乎无法让 Selenium 单击 gif 以继续结帐,我认为除非您创建帐户等,否则您将无法看到该页面,所以我会将html代码粘贴到下面

我正在使用:

  • Python v3.9
  • Chrome v87

这是我练习的网址:

https://www.aria.co.uk/myAria/ShoppingBasket

硒代码:

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.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054

# Click "Add to Basket" or refresh page if out of stock
try:
    element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
    time.sleep(5)
    browser.refresh()
except:
    button = driver.find_element_by_id("addQuantityButton")
    button.click()

# Click basket
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()

# Click checkout
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(@src,'/static/images/checkoutv2.png')]"))).click()

# Login to account
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser@hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(1)
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
driver.implicitly_wait(1)
login.send_keys(Keys.ENTER)

# Click continue checkout
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@src,'/dynres/YnV0dG9uX3NtYWxsLm1lZGl1bQ==/Q29udGludWUgQ2hlY2tvdXQ=.gif')]"))).click()

我需要点击的 gif 的 HTML 代码

<input type="hidden" name="addressID" value="940342">
<div style="text-align:right; margin-top:10px;">
    <input id="formSubmit" class="gaTrackedEvent" class="gaTrackedEvent" data-ga_event_category="checkout" data-ga_event_action="click_continue" data-ga_event_label="deliverydetails" type="image" src="/dynres/YnV0dG9uX3NtYWxsLm1lZGl1bQ==/Q29udGludWUgQ2hlY2tvdXQ=.gif" value="Continue Checkout">
</div>
</form>
</div>

【问题讨论】:

  • 试试password.submit()

标签: python selenium-webdriver xpath css-selectors webdriverwait


【解决方案1】:

你试过它的ID了吗?只要确保全部:)“gaTrackedEvent”类也可能有效。我需要查看 HTML 的其余部分,以确保页面上的 ID 或类名不超过一个。

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "formSubmit"))).click()

【讨论】:

  • 有时是一个简单的 .Click() 不起作用,我也会尝试这样的:browser.find_element_by_id('formSubmit').send_keys(Keys.RETURN)
  • 很遗憾,HTML 元素没有 ID
  • 您应该能够从以下位置获取输入:
【解决方案2】:

那个 xpath 会失败。

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Continue Checkout']"))).click()

密码也改成

passwrd = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[password]")))

【讨论】:

  • 我最初尝试过,但它不会在密码框中输入,所以不得不使用上述方法
【解决方案3】:

要点击Continue Checkout gif 元素,您需要为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.gaTrackedEvent#formSubmit[data-ga_event_category='checkout'][value='Continue Checkout']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='gaTrackedEvent' and @id='formSubmit'][@data-ga_event_category='checkout' and @value='Continue Checkout']"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • 对不起,我应该补充一点,我不能使用 gaTrackedEvent,因为它也在上面的代码中用于不同的 html 元素
  • @StephenBrown 定位器包含除class="gaTrackedEvent" 之外的其他三个属性,因此定位策略唯一地标识元素。你能给我测试结果吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多