使用selenium

时间:2018-05-22 05:48:28

标签: python-3.x selenium-webdriver pycharm selenium-chromedriver

我打算在打开网页后使用selenium删除广告和其他弹出窗口。弹出窗口已被删除,但广告未被删除。执行javascript代码时出现一些错误(使用它删除广告)。没有给出任何理由。此外,对于广告,当我通过键入链接自己打开网页时,广告不会出现,但是当我运行程序时,代码生成的浏览器广告出现(原因我不知道)。我附上了代码和错误。这是代码

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("http://indianexpress.com/columnists/")

// To remove pop-up
later = driver.find_elements(By.CLASS_NAME, "iz_block_button")
later[0].click()

//to remove ad
all_iframes = driver.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
    print("Ad Found\n")
    driver.execute_script("""
    var elems = document.getElementsByTagName('iframe'); 
    for(var i = 0, max = elems.length; i < max; i++)
    { 
        elems[i].visibility=hidden; 
    } 
                         """)
    print('Total Ads: ' + str(len(all_iframes)))
else:
    print('No frames found')

driver.close()

错误: -

Traceback (most recent call last):
  File "/Users/arjungoyal/Desktop/untitled/a.py", line 23, in <module>
    """)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 627, in execute_script
    'args': converted_args})['value']
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: hidden is not defined
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)

请有人说出错误是什么以及如何将其删除。

0 个答案:

没有答案