How to run selenium and behave on a local port? - python

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

Related

How to use selenium to download CDP network history as har

I am working on a project that is built mostly in python it is using selenium to emulate user interaction with a website. Then it downloads the network history through chrome dev tools. I recently ran into a roadblock as I am unable to monitor network data directly through Selenium CDP when using python.(or at least can't find out how to do so.) Nor can I figure out to simply type in commands into the browser.
If anyone knows how to either send keys directly to the browser through selenium (I have not been able to get actionchains to work.) or how to access CDP network and history as .har file in python. Then their help would be greatly appreciated.

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.

Checking current browser url with python

I'm trying to make an app that blocks some acces to certain websites, now i'm stuck thinking how to check the current url. I've tried selenium, but that doesn't work when you change tabs, so i had to try something else. I've been thinking about a chrome addon that checks current url and sends it to my python code, but i don't know how to do it without making any additional server. Any help appreciated.
You can use selenium and web drive manager for this
Install webdrive manager first using
pip install webdrive-manager
Then input the following code
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
print (driver.current_url)

Execute Selenium script from CGI script

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

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