I need to run selenium on a remote server since the server doesn't have a screen I'm using PhantomJS as a web driver. this is the code I Have:
import time
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import re
from selenium import webdriver
url = 'https://wiki.python.org/moin/HowTo/Sorting'
driver_1 = webdriver.PhantomJS()
driver_1.get(url)
content = driver_1.page_source
On my computer, it works fine, but after that, I'll open other pages so I need the code keep running for a while.
I'm running the script from the terminal as:
python my_script.py
And the script will execute on the same terminal from where I'm accessing the server so either I won't be able to disconnect or if a loss the connection the script will stop, how can I handle this issue?
You do not need a read GUI on the server in order to run Selenium with any browser. Selenium can easily run on any server with real browsers like Chrome or Firefox.
Here is some code that should help you make it working:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
driver = webdriver.Chrome() # Or Firefox()
If you want your script to continue executing even if you lose connection to the server, you can easily do this with terminal software like Tmux or screen
Related
At this point I'm very familiar with how to get Selenium and Behave to work together to interact with another website (like typing a phrase into an input box). However, because I have created a fullstack python application that I am able to run on my local port 8000, I would like to have Selenium and Behave interact with my site running on the local port the same as it would with an external website. For instance, if I try to run my application on port 8000 and then run Selenium with the following code:
from selenium.webdriver import Chrome
from selenium.webdriver.support.ui import Select
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("localhost:8000/")
select = Select(driver.find_element_by_xpath('/html/body/header/div[1]/div/div/div/form/div/div[1]/div/input'))
select.select_by_value('1')
I am met with a 500 server error in the browser.
I have followed along with this helpful Medium article. But again, it uses an external website.
Is this a case to use java -jar in the command line? I don't see how this would work given I have not written anything in Java.
Thank you all so much for any guidance.
EDITED TO ADD
Selenium will now open the page (I accidentally had http before localhost)
My solution on this was setting it towards the local IP. In my case it was:
http://127.0.0.1:PORT
I am currently working on a project where we need to run a Selenium Script to automate some processes on an IE browser. We have to do this via Selenium as the website we're run automation on doesn't have an API and ONLY works on an old version of IE, due to certificates.
Thus, I've come up with the following solution:
Create CGI script on EC2 Windows server that contains Selenium code
Parse parameters from the URL to this script, which are then entered into the Selenium fields.
Connect externally every time this Selenium script needs to be run.
I've created the EC2 server, I've allowed for CGI scripts and this works fine with a basic CGI script. As soon as I add the following lines the browser just hangs and does nothing:
browser = webdriver.Ie("C:/IEDriverServer.exe")
browser.get("http://www.google.com")
Does anyone have experience doing something similar, when the browser hangs, the Selenium script executes an IE server background process, but doesn't actually launch IE.
#!C://Python27/python.exe
from datetime import datetime
from selenium import webdriver
print('Content-Type: text/plain')
print('')
print('Hello,s world!')
# Setup default variables
browser = webdriver.Ie("C:/IEDriverServer.exe")
browser.get("http://www.google.com")
I'm using pyvirtualdisplay to run a test with a headless Firefox browser. This is the code I'm using :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from pyvirtualdisplay import Display
display= Display(visible=0, size=(320, 240)).start() # visible=0
display.start()
driver = webdriver.Firefox()
driver.get("https://google.com")
display.quit()
And the traceback that I obtain :
easyprocess.EasyProcessCheckInstalledError: cmd=['Xvfb','-help']
You can't use pyvirtualdisplay on Windows.
It is just a wrapper that calls Xvfb. Xvfb is a headless display server for the X Window System. Windows does not use the X Window System.
For windows users you can use the free VNC utility.. For example if you are running docker you can do it in 3 steps:
Run a docker image that has the standalone firefox server (that has port 5900 exposed for VNC)
$ docker run -d -p 4444:4444 -p 5990:5990 selenium/standalone-firefox-debug
Open VNC and connect to that host localhost:5990, password is 'secret'
Now simply execute your selenium script and you will see what's happening in VNC window live as its happening. Just make sure the script is pointing to your docker standalone server like localhost:4444/wd/hub in order for it to work
I am trying to automation some action on a website using Python and Selenium, this is the sample code I am trying to run, from the Mozilla website for running the Firefox webdriver
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "/usr/bin/firefox-aurora"
driver = webdriver.Firefox(capabilities=caps)
driver.get("https://www.google.com")
driver.quit()
When running this code, the Firefox instance opens normally, and I can even use the instance as if I just opened Firefox normally, but the code 'stops' executing at the driver = webdriver.Firefox(capabilities=caps) line, I tried debugging the code with no luck, the whole execution seems to just stop at this line, and nothing after it is reached!
I am running Python3.5, Selenium version 2.53.6, I have the 'Wires' executable at the /usr/local/bin which is in the environment's PATH, I also have Firefox Aurora version 49.0a2 running on ArchLinux.
Thanks in advance.
[Update:]
I managed to get it to work after all using Firefox 46 (the normal version).
I am running following code in PyCharm
from selenium import webdriver
driver = webdriver.Firefox
Scripts executes successfully with following message in console
C:\Python34\python.exe C:/Users/dev/PycharmProjects/PYLearn/firs_selenium_script.py
Process finished with exit code 0
But Firefox Browser is not opening, I have selenium installed using pip, Do I need additional setting to make this work.
Thanks,
To launch the browser your code should look like:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://your-url')
After the test you will want the close the browser, so add:
self.browser.quit()
Hope this solve your problem.
More info: http://selenium-python.readthedocs.org/getting-started.html
Go to Python Console at the bottom and run your commands and check the error
You can try this:
from selenium import webdriver
browser = webdriver.Chrome()
Browser should be opened , anyways check whether the browser started running or not.
Once, your browser fully loaded, your command will move to next line