Python Selenium Autofill问题

时间:2017-04-24 15:23:02

标签: python selenium selenium-webdriver bots autofill

我正在尝试创建一个自动脚本bot,用于查找特定项目并将项目添加到用户购物车等等。现在我被抓住了,因为无论何时代码自动填写用户信息,电话号码和信用卡号码都会倒退或混淆。

enter image description here

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

# Open chrome web browser and directs to supreme.com

browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')

# Find specific item 

browser.find_element_by_xpath('//*
[@id="container"]/article[12]/div/a/img').click()
WebDriverWait(browser,10).until(
EC.visibility_of_any_elements_located((By.ID,'size')))


# Select size 

Select = Select(browser.find_element_by_id('size'))
Select.select_by_visible_text("Large")

# Add to cart

browser.find_element_by_xpath('//*[@id="add-remove-buttons"]/input').click()
browser.find_element_by_xpath('//*[@id="cart"]/a[2]').click()


# Input user info

Name = browser.find_element_by_id('order_billing_name')
Name.send_keys("Justin Malik")

Email = browser.find_element_by_id('order_email')
Email.send_keys("xxxxxxxxxx@gmail.com")

Telephone = browser.find_element_by_id('order_tel')
Telephone.send_keys("3334449875")

Billing_address = browser.find_element_by_id('bo')
Billing_address.send_keys("13002 lala land CT")

Billing_zip = browser.find_element_by_id('order_billing_zip')
Billing_zip.send_keys("33579")

Billing_City = browser.find_element_by_id('order_billing_city')
Billing_City.send_keys()

Billing_State = browser.find_element_by_id('order_billing_state')
Billing_State.send_keys("TX")



# Credit info

Credit_number = browser.find_element_by_id('cnb')
Credit_number.send_keys("1234 5698 7588 4444")

Credit_month = browser.find_element_by_id('credit_card_month')
Credit_month.send_keys("07")

Credit_year = browser.find_element_by_id('credit_card_year')
Credit_year.send_keys("2021")

Credit_verif = browser.find_element_by_id('vval')
Credit_verif.send_keys("334")


# Accept Terms & Agreements 

terms = browser.find_element_by_id('order_terms').click()

3 个答案:

答案 0 :(得分:1)

首先删除填充文本,然后执行发送键。使用ActionChain类。 尝试以下示例:

from selenium.webdriver.common.action_chains import ActionChains

Telephone_field = browser.find_element_by_id('order_tel')

Creditcard_field = browser.find_element_by_id('cnb')

ActionChains(driver).move_to_element(Telephone_field).click().key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).send_keys("3334449875").perform()

ActionChains(driver).move_to_element(Creditcard_field).click().key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).send_keys("1234 5698 7588 4444").perform()

答案 1 :(得分:0)

您使用的是哪种浏览器?您可以像这样禁用自动填充:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-single-click-autofill");
    options.addArguments("--disable-autofill-keyboard-accessory-view[8]");
    driver = new ChromeDriver(options);

或者尝试设置自己的个人资料,并在开始测试之前尝试删除Cookie! 以上内容取自here

如果上述情况不充分,您可以允许自动填充工作一次,然后快速删除所有自动填充条目。因此,在键入之前调用清理:

browser.find_element_by_xpath('//*[@id="elementWhichWasAutoFilled"]/input').clear()

答案 2 :(得分:0)

您可以尝试使用JavaScript设置每个输入字段的值:

Telephone = browser.find_element_by_id('order_tel')
Credit_number = browser.find_element_by_id('cnb')
browser.execute_script('arguments[0].value="3334449875";', Telephon)
browser.execute_script('arguments[0].value="1234569875884444";', Credit_number)