回复提示();使用Python Webscraper

时间:2019-01-10 15:17:30

标签: javascript python selenium web-scraping urllib

我正在尝试使用Python抓取一个网站,但它给出了HTTP 401错误。在浏览器上访问网站时,单击链接会弹出一个JavaScript提示框,我必须在其中输入“ YES”来查看该网站。我该怎么办?

出于保密原因,我无法共享该网站的链接。

错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python36\lib\urllib\request.py", line 223, in urlopen
  return opener.open(url, data, timeout)
File "C:\Program Files\Python36\lib\urllib\request.py", line 532, in open
  response = meth(req, response)
File "C:\Program Files\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Program Files\Python36\lib\urllib\request.py", line 570, in error
  return self._call_chain(*args)
File "C:\Program Files\Python36\lib\urllib\request.py", line 504, in _call_chain
  result = func(*args)
File "C:\Program Files\Python36\lib\urllib\request.py", line 650, in 
http_error_default
  raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized

1 个答案:

答案 0 :(得分:0)

弹出窗口没有很好的记录,但这应该可以工作:

alert = driver.switch_to.alert
alert.send_keys('YES')
alert.accept()

我认为switch_to.alert创建了一个Alert对象,记录在这里:https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.alert.html#module-selenium.webdriver.common.alert