使用Selenium Webdriver抓取页面时,会出现“弹出窗口”。
在打开页面上,http://www.fanatics.com/search/red%20shoes-我看到一个带有xpath'// * [@ id =“ mm_DesktopInterstitialImage”]'的弹出窗口-但我不想成为使用xpath关闭此警报,并具有一些通用的内容,可以关闭/关闭该警报。这是我到目前为止尝试过的-
from selenium import webdriver
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
url='http://www.fanatics.com/search/red%20shoes'
driver.get(url)
#driver.set_page_load_timeout(300)
driver.implicitly_wait(60)
alert = driver.switch_to_alert()
alert.dismiss
handle=driver.window_handles
print handle
#[u'CDwindow-B1E9C127-719D-ACAA-19DE-1A6FA265A4FF']
根据我从相关示例中了解到的信息,人们通常会切换窗口句柄,但是我的句柄只有一个元素。即,driver.switch_to_window(driver.window_handles[1])
然后driver.close()
再使用driver.switch_to_window(driver.window_handles[1])
我还使用了隐式等待的方式再次移动,因为我不确定是否正在读取警报窗口完全-但这也不起作用。如果可能的话,我不愿意为xpath进行硬编码。
我究竟做错了什么 ?
相关,但对我不起作用:Selenium python如何关闭弹出窗口?
如我所见,它不是警报框!!。当您进入页面时,它只是一个简单的弹出窗口,它出现在主窗口本身中(也无需切换和关闭它)。使用以下代码将其关闭。
driver.find_element_by_xpath('//div[contains(@class,"ui-dialog") and @aria-describedby="dialogContent2"]//button[@title="Close"]').click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句