【问题标题】:How to scrape pop ups using selenium with multiple windows如何使用带有多个窗口的 selenium 抓取弹出窗口
【发布时间】:2022-01-14 18:26:50
【问题描述】:

来自本网站:https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract1.程序首先点击“连接网络”2.然后我想点击“Wallet Connect”.

目前,只有在删除 driver.execute_script('''window.open("http://bings.com","_blank");''') 时,Wallet Connect 才会点击。 driver.switch_to.window(driver.window_handles[0]) 只有在只有一个选项卡时才会切换到包含钱包的弹出窗口。当有另一个选项卡(或任意数量的选项卡)时,如何切换到弹出窗口;即当driver.execute_script('''window.open("http://bings.com","_blank");''') 包含在脚本中时。

    # -*- coding: utf-8 -*-
"""
Created on Fri Dec  3 20:44:06 2021

@author: Main
"""

import numpy as np
import pandas as pd
from bs4 import BeautifulSoup as soup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(executable_path=r'C:\Users\Main\Documents\Work\Projects\Scraping Websites\extra\chromedriver')
 
 driver.get("https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract")
driver.execute_script('''window.open("http://bings.com","_blank");''')#code works only if this is removed

driver.switch_to.window(driver.window_handles[0])
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btnCookie"))).click()
WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#writecontractiframe")))
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#connectStatus"))))

WebDriverWait(driver, 5).until(EC.number_of_windows_to_be(1))
driver.switch_to.window(driver.window_handles[0])
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, """//button[@onclick="connectWalletConnect('writecontractiframe')"]"""))).click()

【问题讨论】:

  • 它只输出2个句柄,每个选项卡一个,没有弹出句柄
  • 弹出窗口也不在 iframe 中。

标签: python selenium selenium-webdriver web-scraping selenium-chromedriver


【解决方案1】:
wait=WebDriverWait(driver,10)
driver.get("https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract")
driver.execute_script('''window.open("http://bings.com","_blank");''')#code works only if this is removed

driver.switch_to.window(driver.window_handles[0])
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btnCookie"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#writecontractiframe")))
elem=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#connectStatus")))
driver.execute_script("arguments[0].click();", elem)
driver.switch_to.default_content

##wait.until(EC.number_of_windows_to_be(1))
driver.switch_to.window(driver.window_handles[0])
wait.until(EC.element_to_be_clickable((By.XPATH, """//button[@onclick="connectWalletConnect('writecontractiframe')"]"""))).click()

您从来没有真正关闭窗口,因为窗口数量为 1。 而且你从来没有离开过 iframe。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多