Reading "Data Wrangling with Python" by J. Kazil I'm at this example about screen reading with Selenium. The code (following the code given by the authors) is like this:
# sele.py
1 import time
2 from selenium import webdriver
8 browser = webdriver.Firefox()
9 browser.get('http://www.fairphone.com/we-are-fairphone/')
11 iframe = browser.find_element_by_xpath("//iframe[@id='twine-iframe-none']")
12 new_url = iframe.get_attribute('src')
13 browser.get(new_url)
14 time.sleep(4)
15
16 all_bubbles = browser.find_elements_by_css_selector('div.content')
17 for elem in all_bubbles:
18 print(elem.text)
I changed some of the book's code:
line 11: original code by the authors of "Data Wrangling" only used //iframe for the xpath expression, which resulted in a blank page
line 14: I added the timeout here, since without timeout all that happened was the return of the command line cursor
line 18: original code works with Python 2, but changing the examples to Python 3 has worked so far (I'm at p. 320 now)
What happens is that a new browser window opens, loads fairphone's homepage, and switches to the iframe part. This is all good. The next bit would be to print out the "bubbles" content, which however doesn't happen. Instead I collect a fairly verbose error message:
Traceback (most recent call last):
File "sele.py", line 16, in module all_bubbles = browser.find_elements_by_css_selector('div.content')
File ".../virtEnv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 449, in find_elements_by_css_selector
return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)
File ".../virtEnv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 778, in find_elements
'value': value})['value']
File ".../virtEnv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File ".../virtEnv/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Permission denied to access property "handleEvent"
Stacktrace:
at <anonymous> (http://apps-cdn.twinesocial.com/js-min/files/player%252Fbugsnag.js+player%252Fjquery-1.11.1.min.js+player%252Fjquery-easing-1.3.js+player%252Fjquery.timeago.js+player%252Fbootstrap-3.1.1.min.js+player%252Fisotope.pkgd.min.js+player%252Fjquery.lazy.min.js+player%252FjMinEmoji-SVG.min.js+player%252Ftheme-base.js+player%252Ftheme-base-utility.js+player%252Ftheme-base-toolbar.js+player%252Ftheme-base-fx.js+player%252Ftheme-base-manage.js+player%252FisInViewport.min.js+player%252FAnimOnScroll.js+player%252Fmodernizr.custom.js+player%252Fselect2.min.js+player%252Fhandlebars.min.js+player%252Ftheme%252Fclassic/v/2.9/t/1471301242.js:2)
I'm using Python 3.5 in a virtual environment (OS is Ubuntu 16). I'm quite stuck here, and documentations like Read the Docs only helped to advance to the part where I am now. I'm aware that there is cool stuff like BeautifulSoup or Scrapy out there, but I would like to do this with Selenium for now.
Edit: Another question was identified as a possible duplicate. That question is within a context of Bugsnag and Perl. Nevertheless I tried the there mentioned workaround by adding
15 active_element = browser.switch_to_active_element()
and changing
16 all_bubbles = active_element.find_elements_by_css_selector('div.content')
which produced the same error message. I noticed that the final part of the error message indeed has bugsnag nested within the final line. I'm not sure how that is related to my setup since I'm not using bugsnag. However, it might be helpful to mention that I use Firefox 48 and Selenium 2.53.6