Làm cách nào để chuyển sang cửa sổ bật lên trong chương trình Selenium dưới đây. Tôi đã tìm kiếm tất cả các giải pháp có thể nhưng không thể có được đầu của tôi xung quanh chúng. Xin vui lòng giúp đỡ!!
from selenium import webdriver
from splinter import Browser
from selenium.webdriver.common.keys import Keys
handle = []
driver = webdriver.Firefox[]
driver.get["//example.com/test.aspx"]
driver.find_element_by_link_text["Site Actions"].click[]
driver.find_element_by_link_text['Edit Page'].click[]
select = driver.find_element_by_id['ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult']
for option in select.find_elements_by_xpath['//*[@id="ctl00_PlaceHolderMain_ctl35_ctl00_SelectResult"]/option']:
if option.text != 'Channel':
option.select[] # select[] in earlier versions of webdriver
driver.find_element_by_id['ctl00_PlaceHolderMain_ctl35_ctl00_RemoveButton'].click[]
parent_h = driver.current_window_handle
#click that activates the popup.
checkIn = driver.find_element_by_id['qaCheckin_anchor'].click[]
# click on the link that opens a new window
handles = driver.window_handles # before the pop-up window closes
driver.remove[parent_h]
driver.switch_to_window[handles.pop[]]
driver.implicitly_wait[10] # seconds
driver.find_element_by_xpath['/html/body/form/div[3]/table/tbody/tr[4]/td/table/tbody/tr[3]/td[2]/input'].click[]
driver.find_element_by_name['btnClose2'].click[]
driver.close[];
# do stuff in the popup
# popup window closes
driver.switch_to_window[parent_h]
# and you're back
driver.switch_to_default_content[]
Hỏi ngày 30 tháng 9 năm 2015 lúc 5:26Sep 30, 2015 at 5:26
import.zeeimport.zeeimport.zee
9152 Huy hiệu vàng9 Huy hiệu bạc13 Huy hiệu đồng2 gold badges9 silver badges13 bronze badges
6
Về mặt trình duyệt, bật lên không phải là một cửa sổ, nó là một cảnh báo. Vì vậy, bạn nên sử dụng sau:
driver.switch_to_alert[]
Đã trả lời ngày 30 tháng 9 năm 2015 lúc 9:04Sep 30, 2015 at 9:04
Mahsum akbasmahsum akbasMahsum Akbas
1.3373 huy hiệu vàng19 Huy hiệu bạc36 Huy hiệu đồng3 gold badges19 silver badges36 bronze badges
2
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
First, we have to get
the current window handle from a webdriver which can be done by:
driver.current_window_handle
Bàn luận
driver.window_handles
Nhiều trang web sử dụng đăng nhập bằng phương tiện truyền thông xã hội để làm cho quá trình đăng nhập dễ dàng cho người dùng. Trong hầu hết các trường hợp, nếu nút được nhấp thì một cửa sổ bật lên mới được mở khi người dùng phải nhập thông tin đăng nhập người dùng của họ. Theo cách thủ công, người ta có thể chuyển Windows trong trình duyệt và nhập thông tin đăng nhập cần thiết để đăng nhập. Nhưng trong trường hợp truy cập web không được giám sát bằng WebDriver, trình điều khiển không thể tự động chuyển Windows. Chúng ta cần thay đổi tay cầm cửa sổ trong trình điều khiển để nhập thông tin đăng nhập trong cửa sổ bật lên. Selenium có chức năng chuyển đổi cửa sổ để truy cập nhiều cửa sổ bằng cùng một trình điều khiển. Đầu tiên, chúng ta phải lấy tay cầm cửa sổ hiện tại từ một webDriver có thể được thực hiện bởi: & nbsp; & nbsp;
driver.switch_to.window[login_page]
Chúng ta cần lưu nó để có được tay cầm cửa sổ hiện tại. Sau khi cửa sổ bật lên xuất hiện, chúng tôi phải lấy danh sách tất cả các tay cầm cửa sổ có sẵn ngay bây giờ. & NBSP; & nbsp;
Note: To run this code selenium library and geckodriver for firefox is required. The installation of selenium can be done using Python third-party library installer pip. To install selenium run this command
pip install selenium
Sau đó, chúng tôi có thể lấy tay cầm cửa sổ của trang đăng nhập từ danh sách này và sau đó chuyển đổi điều khiển. Để chuyển đổi tay cầm cửa sổ, sử dụng: & nbsp; & nbsp;
Let’s see the code for login on
zomato.com using Facebook.
Python3
from
selenium
import
webdriver
from
driver.switch_to_alert[]
1import
driver.switch_to_alert[]
3driver.switch_to_alert[]
4driver.switch_to_alert[]
5 driver.switch_to_alert[]
6driver.switch_to_alert[]
7driver.switch_to_alert[]
5 driver.switch_to_alert[]
9driver.current_window_handle0
driver.current_window_handle1
driver.current_window_handle2
driver.current_window_handle3
driver.current_window_handle4
driver.current_window_handle5
driver.current_window_handle0
driver.current_window_handle1
driver.current_window_handle2
driver.current_window_handle3
driver.window_handles0
driver.current_window_handle5
driver.window_handles2
driver.window_handles3
driver.window_handles4
driver.window_handles5
driver.window_handles6
driver.window_handles7
driver.window_handles8
driver.switch_to_alert[]
5 driver.switch_to.window[login_page]0
driver.switch_to.window[login_page]1
driver.switch_to.window[login_page]2
driver.switch_to_alert[]
5 driver.switch_to.window[login_page]4
driver.switch_to.window[login_page]5
driver.switch_to.window[login_page]6
driver.switch_to.window[login_page]7
driver.switch_to.window[login_page]8
driver.switch_to.window[login_page]9
driver.switch_to_alert[]
5pip install selenium1
pip install selenium2
driver.switch_to_alert[]
5 pip install selenium4
pip install selenium5
driver.switch_to.window[login_page]6
driver.switch_to.window[login_page]7
pip install selenium8
driver.switch_to.window[login_page]9
driver.switch_to_alert[]
5pip install selenium1
from
2
driver.switch_to_alert[]
5 pip install selenium4
pip install selenium5
driver.current_window_handle3
from
7from
8driver.current_window_handle3
selenium
0selenium
1driver.current_window_handle3
selenium
3driver.current_window_handle5
selenium
5
driver.current_window_handle0
selenium
7driver.current_window_handle2
selenium
9
driver.switch_to_alert[]
5 driver.current_window_handle3
import
2___driver.switch_to.window[login_page]6
driver.switch_to.window[login_page]7
import
6import
7import
8import
9webdriver
0
Đầu ra: & nbsp; & nbsp;