使用Selenium将键发送到输入字段

时间:2017-05-18 05:06:04

标签: python selenium shopify sendkeys

我正在尝试点击付款信息的输入字段,然后将密钥提交到这些输入字段。我发现他们在iframe,但我没有运气切换到不同的帧。我附上了html的图片以及页面的样子。任何帮助,将不胜感激!这是我的代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import ElementNotVisibleException
import time

driver = webdriver.Chrome()
print('Browser Successfully Iitialized!')

driver.get('https://kith.com/products/new-balance-696-beige-pink')
input()
print('Beginning Checkout Process!')
print('Posting Customer Information...')
while True:
    try:
        driver.find_element_by_id('g-recaptcha-response')
        print('Captcha Detected!')
        print('Solve Captcha...')
        captcha = True
        break
    except NoSuchElementException:
        print('No Captcha Detected!')
        captcha = False
        break

email_field = driver.find_element_by_xpath('//*[@id="checkout_email"]').send_keys('johndoe44@gmail.com')
fname_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_first_name"]').send_keys('John')
lname_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_last_name"]').send_keys('Doe')
address_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_address1"]').send_keys('12345 Street')
city_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_city"]').send_keys('City')
state_dropdown = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_province"]/option[22]').click()
zipcode_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_zip"]').send_keys('46001')
phone_field = driver.find_element_by_xpath('//*[@id="checkout_shipping_address_phone"]').send_keys('1234567891')

if captcha == True:
    while captcha is True:
        time.sleep(3)
        try:
            cont_shipping = driver.find_element_by_name('button').click()
            button = driver.find_element_by_class_name('btn__content')
            if button.text == 'Continue to payment method':
                print('Captcha Solved!')
                captcha = False
        except:
            captcha = True

driver.find_element_by_name('button').click()
print('Posting Shipping Information...')        
shipping_url = driver.current_url
shipping_url.replace("previous_step=contact_information&step=shipping_method", "")
payment_url = shipping_url + 'previous_step=shipping_method&step=payment_method'
driver.get(payment_url)
print('Posting Payment Information...')
try:
    el = driver.find_element_by_xpath('//*[@id="payment-gateway-subfields-1427382"]/div[3]/div[2]')
    el.click()
    el.send_keys('John Doe')
except ElementNotVisibleException:
    print('Error')

Image of the html and screen of the input field/fields

1 个答案:

答案 0 :(得分:0)

点击PayPal按钮后使用以下代码:

<button onclick="onTimer()">Clickme</button>
<div id="mycounter"></div>
<script>
i = 60;
function onTimer() {
document.getElementById('mycounter').innerHTML = i;
i--;
if (i < 0) {
alert('You lose!');
}
else {
setTimeout(onTimer, 1000);
}
}
</script>

付款网关选项(电子邮件,密码..)在iframe下显示,因此首先切换到iframe,然后执行操作