[英]I can't get the webdriver to fill space with text, but i've tried everything on this website
我正在尝试从列表存储为键 [] 或 K 的配置文件中输入电子邮件地址。我试图通过 xpath 找到元素,然后发送键 k 电子邮件,但这没有用。 我尝试使用动作链来标记空间。 我尝试使用等待点击,就像我用于其他所有内容一样。 我还尝试按名称定位然后发送密钥并通过 class 名称发送密钥定位。 似乎没有任何工作。
from Config import keys
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
def order(k):
email_tab = 2 #Number of times to hit tab for email
# Load the Driver
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
wait = WebDriverWait(driver, 10)
# Choose the bottle via xpath
#bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="thumb-bbt2020package"]/div/div[1]/div/div/img')))
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
wait
#Adding the item to the cart
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
# Going to the cart
enter_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'custom-cart-button')))
enter_cart.click()
wait
driver.get(k['checkout_url'])
wait
# Going to checkout screen
checkout_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-editable-button CheckoutButton-checkoutButton-3yWY2 checkout-button')))
checkout_cart.click()
wait
#Enter the email
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * email_tab)
actions.perform()
#email_field.send_keys(Keys.ENTER)
wait
{keys = { "product_url": "sideprojectbrewing.com/shop?category=Beer+Release", "checkout_url": "sideprojectbrewing.com/checkout", }
在此代码中,问题不是email
字段而是 button checkout
。
您使用两种不同的方法来加载带有checkout
的页面。
首先你使用
get(k['checkout_url'])
后来你使用
checkout_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-editable-button.CheckoutButton-checkoutButton-3yWY2.checkout-button')))
checkout_cart.click()
这会产生问题。 您必须使用其中之一,而不是同时使用两者。 在错误页面上使用第一种方法第二种方法搜索按钮后。
顺便一提:
如果您使用class
来搜索元素,那么像'sqs-editable-button CheckoutButton-checkoutButton-3yWY2 checkout-button'
这样的多类存在一个问题。 Selenium
将其转换为 CSS 选择器,但它将此字符串视为一个 class 并添加.
仅在此字符串的开头之前
.sqs-editable-button CheckoutButton-checkoutButton-3yWY2 checkout-button
但它应该把.
也在所有名字之间
.sqs-editable-button.CheckoutButton-checkoutButton-3yWY2.checkout-button
所以你必须把.
手动命名之间(但不要在开头添加)
sqs-editable-button.CheckoutButton-checkoutButton-3yWY2.checkout-button
所以你会有
checkout_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-editable-button.CheckoutButton-checkoutButton-3yWY2.checkout-button')))
checkout_cart.click()
为我工作的代码
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
def order(k):
email_tab = 2 #Number of times to hit tab for email
# Load the Driver
print('main page')
driver = webdriver.Chrome()
driver.get(k['product_url'])
wait = WebDriverWait(driver, 10)
# Choose the bottle via xpath
print('bottle page')
#bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="thumb-bbt2020package"]/div/div[1]/div/div/img')))
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
#Adding the item to the cart
print('add to cart')
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
# Going to the cart
print('go to cart')
enter_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'custom-cart-button')))
enter_cart.click()
#print('go to checkout')
#driver.get(k['checkout_url'])
# Going to checkout screen
print('go to checkout')
checkout_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-editable-button.CheckoutButton-checkoutButton-3yWY2.checkout-button')))
checkout_cart.click()
#Enter the email
print('email')
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * email_tab)
actions.send_keys('hello@world.com')
actions.perform()
#email_field.send_keys(Keys.ENTER)
import time
time.sleep(3)
# --- main ---
keys = {
"product_url": "https://sideprojectbrewing.com/shop?category=Beer+Release",
"checkout_url": "https://sideprojectbrewing.com/checkout",
}
order(keys)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.