Execute Selenium script from CGI script - python

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")

Related

Can selenium recognize a state that a web browser is in and run a script in it?

I am wondering if there is a way for selenium to recognize the state of a browser and run a script. For example, if I have selenium open chrome and I go to Twitter selenium recognizes that I'm on Twitter and types in the search bar a name. In python btw.

How can I have a python selenium webdriver script run 24/7?

I'm new to programming, this being my first post, but I was wondering if there was a way to make selenium run while my computer is turned off? I made an instagram bot with selenium webdriver python but it only runs when I run it from my computer and turns off when I turn off my computer. I have seen post saying to host it in PythonAnywhere which I tried but selenium requires a webdriver (im using firefox so gekodriver). Is there a way of fixing this or is there another way of making an insagram bot without selenium webdriver?
Thank you
You can use virtual machine and host a server and then through DevOps pipelines (Azure Pipelines/ Jenkins) can make your scripts run without manual intervention.

How to run selenium and behave on a local port?

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

Schedule selenium to run on a remote server

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

How to run Selenium Scripts in webservers?

I have written a few Selenium [Python] webdriver scripts, and while they are running fine on my system, they are not running on my website's server. It is showing errors with Firefox. Firefox is installed on the server. The webserver is Ubuntu. What do I have to do to run those scripts? Please help, I am a newbie.
Selenium requires a running browser and browsers need some kind of X server in order to run. There are many types of X servers and one of them is Xvfb aka X virtual framebuffer that does all the operations in memory and thus requires no screen.
In Wikipedia you could find very nice examples.
This is a nice example too.
You probably need to open the browser headless when executing your scripts on the server.
Here is the Java code for Firefox (Python code should be similar):
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
WebDriver openHeadless() throws Exception
{
FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
return new FirefoxDriver(binary,null); // or 'binary,profile' if you have a profile
}
Make sure that you have Firefox installed on the server at /usr/local/bin/firefox.
If you want simply do web browser testing you can use libraries like Casper JS this creates a server side browser for web browser testing, it doesn't need a display driver.
I ended up using pyvirtualdisplay which is a Python wrapper for Xvfb and Xephyr. If anyone is having same issues [and I am sure newbies will], You can try THIS. Also you can use xvfbwrapper. Tutorial for xvfbwrapper HERE.

Categories

Resources