Python 关闭弹出广告。窗口
我开始学习Selenium(使用Python),当驱动程序打开网站时,会显示弹出广告。我尝试使用以下方法关闭它:Python 关闭弹出广告。窗口,python,selenium,Python,Selenium,我开始学习Selenium(使用Python),当驱动程序打开网站时,会显示弹出广告。我尝试使用以下方法关闭它: try: if driver.switch_to_alert() != None: clink = driver.find_element_by_id("CloseLink") clink.click() driver.implicitly_wait(10) except Exception, e: print e 我发现它有一个与提到的
try:
if driver.switch_to_alert() != None:
clink = driver.find_element_by_id("CloseLink")
clink.click()
driver.implicitly_wait(10)
except Exception, e:
print e
我发现它有一个与提到的ID链接,它似乎达到它(在浏览器上出现点),但不关闭广告。。。因此,无法访问is下的元素。
我的问题是:
元素可点击”
”:
尝试等待,直到“元素可点击”
”:
:-)(下一个)工作解决方案是:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
def my_proxy(PROXY):
"""
"""
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = PROXY
prox.socks_proxy = PROXY
prox.ssl_proxy = PROXY
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = False
prox.add_to_capabilities(capabilities)
# in my Debian Linux, Firefox is here:
binary = FirefoxBinary('/usr/lib/firefox-esr/firefox-esr')
return webdriver.Firefox(firefox_binary=binary,
capabilities=capabilities)
# PROXY settings:
# -----------------------------
# http://www.freeproxylists.net/
# -----------------------------
driver = my_proxy('52.163.62.13:80')
# to test, if proxy working:
# driver.get("http://whatismyip.otg") # "https://www.iplocation.net")
# exit()
driver.get("http://www.iswinoujscie.pl/artykuly/51291")
# check if expected page is open:
assert "iswinoujscie.pl" in driver.title
# with test as end-user:
try:
if EC.alert_is_present():
print 'EC.alert_is_present()'
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID, "CloseLink"))).click()
else:
print 'hmm... alert not present'
except Exception, e:
print '-->', e
edit_name = driver.find_element_by_id("nickname")
edit_name.clear()
edit_name.send_keys(u"marinio")
tarea_content = driver.find_element_by_id("commentContent")
tarea_content.clear()
tarea_content.send_keys(u"Ale tu fajna pogoda :-) a jak u was?")
button_send = driver.find_element_by_id("submit")
# only after alert-adverb is closed:
button_send.click()
# or with JSexecutor:
driver.execute_script("document.getElementById('submit').click()")
'''
expected reply:
-----------
[...]
Komentarz zostanie dodany
[...]
'''
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.XPATH, "//body"), u'Komentarz zostanie dodany'))
# driver.close() # close webbrowser
感谢@DurdenP@JeffC的帮助(下一步)工作解决方案是:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
def my_proxy(PROXY):
"""
"""
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = PROXY
prox.socks_proxy = PROXY
prox.ssl_proxy = PROXY
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = False
prox.add_to_capabilities(capabilities)
# in my Debian Linux, Firefox is here:
binary = FirefoxBinary('/usr/lib/firefox-esr/firefox-esr')
return webdriver.Firefox(firefox_binary=binary,
capabilities=capabilities)
# PROXY settings:
# -----------------------------
# http://www.freeproxylists.net/
# -----------------------------
driver = my_proxy('52.163.62.13:80')
# to test, if proxy working:
# driver.get("http://whatismyip.otg") # "https://www.iplocation.net")
# exit()
driver.get("http://www.iswinoujscie.pl/artykuly/51291")
# check if expected page is open:
assert "iswinoujscie.pl" in driver.title
# with test as end-user:
try:
if EC.alert_is_present():
print 'EC.alert_is_present()'
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.ID, "CloseLink"))).click()
else:
print 'hmm... alert not present'
except Exception, e:
print '-->', e
edit_name = driver.find_element_by_id("nickname")
edit_name.clear()
edit_name.send_keys(u"marinio")
tarea_content = driver.find_element_by_id("commentContent")
tarea_content.clear()
tarea_content.send_keys(u"Ale tu fajna pogoda :-) a jak u was?")
button_send = driver.find_element_by_id("submit")
# only after alert-adverb is closed:
button_send.click()
# or with JSexecutor:
driver.execute_script("document.getElementById('submit').click()")
'''
expected reply:
-----------
[...]
Komentarz zostanie dodany
[...]
'''
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.XPATH, "//body"), u'Komentarz zostanie dodany'))
# driver.close() # close webbrowser
谢谢你的帮助@DurdenP@JeffC谢谢你DurdenP-这很有效,但是第二个问题呢?我想这是不可能的,我是对的?如果你要单击你正在等待的元素,你应该等待它可单击,
element\u to\u be\u clickable()
。存在只是意味着它在DOM中。如果页面加载缓慢,可能会出现间歇性故障。此外,您也可以等待(By.ID,“CloseLink”)
可单击。等待adPopUp7没有多大意义,然后点击CloseLink
。请稍等,然后单击CloseLink
@WikS.eu以回答第二个问题,是的。。。您可以使用JavascriptExecutor单击弹出窗口下的元素。如果您编写自动化来测试用户场景,那么您通常应该避免使用JavascriptExecutor,因为它允许您做用户不能做的事情,比如单击弹出窗口下的某个东西。但是,如果您不测试用户场景,那么就可以使用它。谢谢DurdenP-它是有效的,但是第二个问题呢?我想这是不可能的,我是对的?如果你要单击你正在等待的元素,你应该等待它可单击,element\u to\u be\u clickable()
。存在只是意味着它在DOM中。如果页面加载缓慢,可能会出现间歇性故障。此外,您也可以等待(By.ID,“CloseLink”)
可单击。等待adPopUp7没有多大意义,然后点击CloseLink
。请稍等,然后单击CloseLink
@WikS.eu以回答第二个问题,是的。。。您可以使用JavascriptExecutor单击弹出窗口下的元素。如果您编写自动化来测试用户场景,那么您通常应该避免使用JavascriptExecutor,因为它允许您做用户不能做的事情,比如单击弹出窗口下的某个东西。但是,如果您不测试用户场景,那么可以使用它。