简体   繁体   中英

How can I switch between different sites with selenium/python?

I'm almost finished with my script that automatically creates a new paypal account. But I want the script to use information from another site before quitting the driver.

(I want the script to open Google mail and then make a new Account and then get the information about the Google E-Mail [eg: E-mail = jamescharles@gnail.com] and paste it into the Paypal E-mail adress bar.

Is this possible?

Can I define Webpages and then make the script to switch between them fast?

Here's my whole code:

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


browser = webdriver.Chrome(executable_path=r"C:\Users\ASUS\Desktop\Name\Pycharm\chromedriver.exe")

browser.get("https://www.paypal.com/welcome/signup?country.x=GB")

try:
    Recap = WebDriverWait(browser, 90).until(
        EC.presence_of_element_located((By.ID, "paypalAccountData_firstName"))
    )
finally:
    browser.forward()


browser.find_element_by_id("acceptAllButton")
cookies = browser.find_element_by_id("acceptAllButton")
cookies.click()


browser.find_element_by_id("paypalAccountData_countryselector")
GB = browser.find_element_by_id("paypalAccountData_countryselector")
GB.click()

browser.find_element_by_id("paypalAccountData_firstName")
Vorname = browser.find_element_by_id("paypalAccountData_firstName")
Vorname.click()
Vorname.send_keys("James")

browser.find_element_by_id("paypalAccountData_lastName")
Nachname = browser.find_element_by_id("paypalAccountData_lastName")
Nachname.click()
Nachname.send_keys("Charles")

browser.find_element_by_id("paypalAccountData_email")
email = browser.find_element_by_id("paypalAccountData_email")
email.click()
email.send_keys("jamescharles@gmail.com")


browser.find_element_by_id("paypalAccountData_password")
Passwort = browser.find_element_by_id("paypalAccountData_password")
Passwort.click()
Passwort.send_keys("jamescharles12!")

browser.implicitly_wait(3)
browser.execute_script("window.open('http://www.google.de','new window')")
print("opened new tab")

try:
    Recap = WebDriverWait(browser, 120).until(
        EC.presence_of_element_located((By.ID, "paypalAccountData_addressSuggest"))
    )
finally:
    browser.forward()
    print("ReCaptcha solved !")
    print("Script goes on.")


browser.find_element_by_id("paypalAccountData_addressSuggest")
Adresse = browser.find_element_by_id("paypalAccountData_addressSuggest")
Adresse.click()
Adresse.send_keys("SW1A 1BD")

browser.find_element_by_id("paypalAccountData_address2")
Adresse2 = browser.find_element_by_id("paypalAccountData_address2")
Adresse2.click()
Adresse2.send_keys("Warwick House")

browser.find_element_by_id("paypalAccountData_address1")
ADL1 = browser.find_element_by_id("paypalAccountData_address1")
ADL1.click()
ADL1.send_keys("St. James's Palace")

browser.find_element_by_id("paypalAccountData_city")
City = browser.find_element_by_id("paypalAccountData_city")
City.click()
City.send_keys("London")

browser.find_element_by_id("paypalAccountData_phoneType")
Number = browser.find_element_by_id("paypalAccountData_phoneType")
Number.click()

browser.find_element_by_name("/paypalAccountData/phoneNumber")
Number1 = browser.find_element_by_id("/paypalAccountData/phoneNumber")
Number.click()
Number.send_keys("87536718921")

browser.find_element_by_id("paypalAccountData_dob")
DateOfBirth = browser.find_element_by_id("paypalAccountData_dob")
DateOfBirth.click()
DateOfBirth.send_keys("02041996")

browser.find_element_by_id("paypalAccountData_oneTouchCheckbox")
Checkout = browser.find_element_by_id("paypalAccountData_oneTouchCheckbox")
Checkout.click()

browser.find_element_by_id("paypalAccountData_marketingOptIn")
Checkout2 = browser.find_element_by_id("paypalAccountData_marketingOptIn")
Checkout2.click()

browser.find_element_by_id("paypalAccountData_tcpa")
Checkout3 = browser.find_element_by_id("paypalAccountData_tcpa")
Checkout3.click()
driver.get("https://www.paypal.com/welcome/signup?country.x=GB")

#get the window handle after the window has opened
window_before = driver.window_handles[0]

#open a new window
driver.execute_script("window.open('https://accounts.google.com/signup', 'new window')")

#get the window handle after a new window has opened
window_after = driver.window_handles[1]

#switch on to new child window
driver.switch_to.window(window_after)

#switch back to original window
driver.switch_to.window(window_before)

You could do something like this to switch from to and from a window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM