簡體   English   中英

管理新的 window selenium

[英]Managing the new window selenium

我想寫一個代碼來登錄“起源”平台

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.binary_location = "C:/Program Files (x86)/BraveSoftware/Brave-Browser/Application/brave.exe"
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.origin.com/irl/en-us/store")

time.sleep(5)
Menu_Button = driver.find_element_by_xpath("//*[@id='shell']/section/div/nav/div/div[1]/div[2]").click()
Sign_in_Button = driver.find_element_by_xpath("//div[@class='origin-cta-primary origin-telemetry-cta-primary']").click()
time.sleep(10)
Email_Adress = driver.find_element_by_xpath("//input[@name='email']").send_keys("Deneme")
Password = driver.find_element_by_xpath("//input[@name='password']").send_keys("Deneme123")
Login_Button = driver.find_element_by_xpath("//a[@id='logInBtn']").click()

按“登錄”按鈕打開一個新的 window 輸入 id 密碼,但我無法管理它

在“Sign_in_Button”之后

您可以使用 driver.window_handles 訪問新的windows ,這只是一個包含所有驅動程序當前 windows 句柄的列表。 如果驅動程序只有主 window 和 window 中的登錄名,則登錄名 window 將是 driver.window_handles[1]。

您可以將驅動程序切換到此 window 與

driver.switch_to.window(driver.window_handles[1])

之后,您應該能夠處理您的代碼

Email_Adress = driver.find_element_by_xpath("//input[@name='email']").send_keys("Deneme")
....

我記得讀過這個,但我不太記得了,這可能會為你指明正確的方向

https://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames

browser=webdriver.Firefox()
[...]
browser.switch_to_window(browser.window_handles[1])
browser.switch_to_window(browser.window_handles[0])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM