Why does not input.send_keys () work in my Selenium WebDriver Python script when run as www-data?

I have a Python script that uses Selenium WebDriver (with PyVirtualDisplay as the display) to enter Flickr.

When I run it as myself on my Debian server, it works fine. (Im sudoer, but I do not use sudo when running the script.)

When I run it as the www-data user (this is what it will run as ultimately because I want to run it from the Django website), I have two problems: one small, one big:

  • (Small): calling webdriver.Firefox() takes 30-45 seconds to return, compared to 2 seconds when I run as myself.
  • (Large): The script cannot enter Flickr. To log in, I find the username and password fields on the Flickr login page (http://www.flickr.com/signin/) and use element.send_keys() to enter the username and password. Although Selenium seems to find elements (i.e., No NoSuchElementException thrown), the values ​​are not entered into the fields when the script is executed as www-data (according to the screenshots that I use with browser.save_screenshot ), unlike of when the script runs like me.

Why send_keys() not work when the script is run as www-data ? (And is this due to the fact that the browser launches much more time?)

+4
source share
4 answers

Maybe you have something else in your environment.

Try copying the ~/.bashrc example to /home/www-data

If this is not enough, run this command both as the current user and as www-data :

 strace -tt -f -s 1000 -o /tmp/trace ./script.py 

And paste it (filter your usernames / passwords) somewhere.

We will see what happens.

+2
source

Sometimes Firefox does some nasty plugin compatibility checks at startup. Since each user may have a different set of browser plug-ins, this may cause a difference in launch time. You can try to synchronize your Firefox profiles between users.

Then, are you sure that Firefox, as user www data, has proper access to the network / internet? Can you confirm that the Flickr site is loading correctly through SeleniumHQ? "The script failed to enter Flickr" is too impenetrable. More information on why this is happening can instantly identify a problem.

Edit: Sorry, I just realized that there shouldn't be a difference in profiles, because Selenium creates one. However, my second point may be useful, so I will not delete this answer.

+1
source

A few more things to consider:

  • Did you fail to start firefox manually from the www-data account once and make sure that Firefox does not update itself before each script execution? Once I ran into this problem with Selenium RC on Windows and had to complete the upgrade before running the script with the updated binary code.

  • As a workaround, I think you could try running the script as a www-data user, but connecting remotely to the webdriver server running in your login (aka net mode). Will this work for you?

+1
source

I would suggest getting the latest chrome from google and try input.send_keys () instead in this browser.

Sometimes some webdriver features break down with new releases. If you are set up to test with firefox, you might be lucky with an older / newer version of selenium webdriver.

I remember that I had a similar problem with send_keys () on mac. My problem was that send_keys () did not work in some modal windows after updating selenium webdriver. I fixed this by returning to an older web director who I knew to work with. However, I used Ruby and not Python to drive webdriver.

sometimes it can also be a problem getting the correct ENV variables in your shell if you use it as another user. I would suggest trying to troubleshoot and verify that all shell ENV variables are set correctly in accordance with www data.

0
source

Source: https://habr.com/ru/post/1437899/


All Articles