from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
browser = webdriver.Safari()
browser.get('https://www.supremenewyork.com/shop/jackets/h90p8mrfk')
sleep(2)
addToCart = browser.find_element_by_name('commit')
addToCart.click()
sleep(2)
browser.find_element_by_xpath('//a[@class="button checkout"]').click()
sleep(2)
browser.find_element_by_id('order_billing_name').send_keys('Name')
browser.find_element_by_id('order_email').send_keys('email')
browser.find_element_by_id('order_tel').send_keys('phone number')
browser.find_element_by_id('bo').send_keys('address')
browser.find_element_by_id('order_billing_zip').send_keys('zip')
Select(browser.find_element_by_id('state_label')).select_by_visible_text('state')
此代码中的所有内容似乎都可以正常工作,直到需要填写运输/计费信息为止。我收到一个奇怪的错误,我无法在互联网上找到有关其含义的任何答案:
selenium.common.exceptions.WebDriverException:消息:未知 发生错误:发生JavaScript异常:参数1(“元素”) Window.getComputedStyle的元素必须是Element的实例
我要完成的自动化过程的步骤是:
1)转到实际网页(检查)
2)将产品添加到购物车(检查)
3)单击结帐按钮(“检查”)
4)输入帐单/运输信息
答案 0 :(得分:0)
使用WebDriverWait()
比使用sleep()
更好。通过ID
进行选择不起作用,请使用XPATH
# -*- coding: utf-8 -*-
from selenium import webdriver
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.support.ui import Select
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('https://www.supremenewyork.com/shop/jackets/qfc4mhl9g')
addToCart = browser.find_element_by_name('commit')
addToCart.click()
# wait max 10 second
checkOut = WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[@class="button checkout"]' ))
)
checkOut.click()
WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'order_billing_name'))
)
browser.find_element_by_xpath('//input[@id="order_billing_name"]').send_keys('Name')
browser.find_element_by_xpath('//input[@id="order_email"]').send_keys('email')
browser.find_element_by_xpath('//input[@id="order_tel"]').send_keys('phone number')
browser.find_element_by_xpath('//input[@id="order_billing_zip"]').send_keys('zip')
browser.find_element_by_xpath('//input[@id="order_billing_address"]').send_keys('address')
state = Select(browser.find_element_by_xpath('//*[@id="order_billing_state"]'))
state.select_by_visible_text('茨城県')