3

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

1

2 Answers 2

1

switch_to_active_element() has been deprecated.

So, it should be:

 active_element = browser.switch_to.active_element

you can refer this

Sign up to request clarification or add additional context in comments.

Comments

0

I encounter the exact same error when using

  • Selenium 3.141.0
  • Firefox 52.9.0
  • Raspbian/Debian 9

The above browser.switch_to.active_element doesn't work for me and throws an exception:

  File "/home/pi/.pyenv/versions/venv37/lib/python3.7/site-packages/myproject/uploader.py", line 102, in login
    element = self.driver.switch_to.active_element
  File "/home/pi/.pyenv/versions/venv37/lib/python3.7/site-packages/selenium/webdriver/remote/switch_to.py", line 44, in active_element
    return self._driver.execute(Command.GET_ACTIVE_ELEMENT)['value']
  File "/home/pi/.pyenv/versions/venv37/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/pi/.pyenv/versions/venv37/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Permission denied to access property "handleEvent"

Thanks for the tips of bugsnag-js, I guess this should be fixed in selenium itself, so I tried to upgrade it to 4.0.0a3 and problem solved! The version 3.141.0 is the latest stable but it releases on Nov 1, 2018 (two years old at this point). So here is my solution:

pip install selenium==4.0.0a3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.