如何用Selenium/Python切换网站?PayPal注册联动Gmail方案问询
Hey there! Let's break down your questions and fix up your script step by step.
问题1:如何使用Selenium/Python在不同网站之间切换?
There are two common scenarios for switching between websites in Selenium, and here's how to handle both:
- Same window, different site: Just call
browser.get("https://your-new-site.com")directly. This will navigate the current window to the new website, replacing the previous page. - Multiple tabs/windows: When you open a new tab (like with
window.open()), you need to switch to its handle first:- Get all active window handles using
window_handles(returns a list of string IDs) - Switch to the target window with
switch_to.window()
- Get all active window handles using
Example code for tab switching:
# Open a new tab with Gmail browser.execute_script("window.open('https://mail.google.com','_blank')") # Get list of window handles all_windows = browser.window_handles # Switch to the new tab (last item in the list is usually the new one) browser.switch_to.window(all_windows[-1]) # Do your Gmail actions here... # Switch back to the original PayPal tab browser.switch_to.window(all_windows[0])
问题2:从Gmail获取邮箱并填入PayPal是否可行?
Absolutely! This is totally achievable with tab/window switching. Your current script has a good start, but let's fix some issues and add the Gmail integration logic.
First, let's address some bugs in your existing code:
- The
browser.forward()calls don't make sense here —forward()navigates forward in the browser history, which isn't needed after waiting for an element. - You have duplicate element lookups (e.g.,
browser.find_element_by_id("acceptAllButton")twice) — you can just store the element once. - The phone number element selector is broken:
Number1 = browser.find_element_by_id("/paypalAccountData/phoneNumber")— that ID is likely incorrect; check the actual HTML for the right ID or useBy.NAMEsince you already referenced the name attribute.
Here's a revised version of your script with the Gmail integration:
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 # Initialize browser browser = webdriver.Chrome(executable_path=r"C:\Users\ASUS\Desktop\Name\Pycharm\chromedriver.exe") browser.maximize_window() # Navigate to PayPal signup browser.get("https://www.paypal.com/welcome/signup?country.x=GB") # Wait for and accept cookies WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "acceptAllButton")) ).click() # Fill in basic info WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "paypalAccountData_firstName")) ).send_keys("James") browser.find_element(By.ID, "paypalAccountData_lastName").send_keys("Charles") # Open Gmail in new tab to create/get email browser.execute_script("window.open('https://mail.google.com/mail/u/0/#signup','_blank')") all_windows = browser.window_handles # Switch to Gmail tab browser.switch_to.window(all_windows[-1]) # --- Add your Gmail account creation logic here --- # Example steps (you'll need to adjust selectors based on Gmail's current UI): # WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Create account']"))).click() # Fill in name, choose username, get the generated email (e.g., jamescharles123@gmail.com) # generated_email = browser.find_element(By.ID, "username").get_attribute("value") + "@gmail.com" # For this example, we'll use a placeholder email (replace with your actual Gmail logic) generated_email = "jamescharles@gmail.com" # Switch back to PayPal tab browser.switch_to.window(all_windows[0]) # Fill in the generated email WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "paypalAccountData_email")) ).send_keys(generated_email) # Continue filling out PayPal form browser.find_element(By.ID, "paypalAccountData_password").send_keys("jamescharles12!") WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.ID, "paypalAccountData_addressSuggest")) ).send_keys("SW1A 1BD") browser.find_element(By.ID, "paypalAccountData_address2").send_keys("Warwick House") browser.find_element(By.ID, "paypalAccountData_address1").send_keys("St. James's Palace") browser.find_element(By.ID, "paypalAccountData_city").send_keys("London") # Fix phone number selector WebDriverWait(browser, 10).until( EC.element_to_be_clickable((By.NAME, "/paypalAccountData/phoneNumber")) ).send_keys("87536718921") browser.find_element(By.ID, "paypalAccountData_dob").send_keys("02041996") # Handle checkboxes browser.find_element(By.ID, "paypalAccountData_oneTouchCheckbox").click() browser.find_element(By.ID, "paypalAccountData_marketingOptIn").click() browser.find_element(By.ID, "paypalAccountData_tcpa").click() # Optional: Wait for reCAPTCHA (you'll need to solve it manually or use a service) # WebDriverWait(browser, 120).until(EC.presence_of_element_located((By.ID, "some-recaptcha-element")))
Key notes for your Gmail integration:
- Gmail's signup UI changes occasionally, so you'll need to inspect the elements to get the correct selectors.
- If you're automating Gmail account creation, be aware that Google has anti-bot measures — you might hit CAPTCHAs or account restrictions. Consider using a temporary email service instead if you just need disposable emails.
内容的提问来源于stack exchange,提问作者Razorbackfire




