Checking current browser url with python - 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)

Related

Scrapy-Selenium: Chrome Driver does not load page

I have two projects, one with Selenium and one using Scrapy-Selenium, which fits into a Scrapy spider program format but uses Selenium for automation.
I can get the Chromedriver to load the page I want for the basic Selenium program, but something about the second project (with Scrapy) prevents it from loading the URL. Instead it's stuck at showing data:, in the URL bar.
First project (works fine):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="./chromedriver")
driver.get("https://ricemedia.co")
Second project (doesn't load page):
import scrapy
from scrapy_selenium import SeleniumRequest
from selenium import webdriver
import time
class ExampleSpider(scrapy.Spider):
name = 'rice'
def start_requests(self):
yield SeleniumRequest(
url="https://ricemedia.co",
wait_time=3,
callback=self.parse
)
def parse(self, response):
driver = webdriver.Chrome(executable_path="./chromedriver")
driver.maximize_window()
time.sleep(20)
I have browsed StackOverflow and Google, and the two most common reasons are outdated Chrome Drivers and missing http in the URL. Neither is the case for me. The path to chromedriver seems alright too (these two projects are in the same folder, along with the same chromedriver). Since one works and the other doesn't, it should have something to do with my Scrapy-Selenium spider.
I should add that I have installed Scrapy, Selenium and Scrapy-Selenium locally in my virtual environment with pip, and I doubt it's an installation issue.
Please help, thanks!
You can use another method to install chrome driver:
First of all install Webdriver manager using following pip install webdriver-manager or use maven dep to get it
Then code:
# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

Selenium in python - Browser closing immediately after open testprogamm

As soon as I run my program, google opens (so far so good) but this window closes immediately after the program has run. I already installed the chromdriver (also matches the version of the search engine) and put it in the script folder in Python. Can someone help me?
Here is my Code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://google.com")
You need to specify an excecutable path for a chrome driver to be able to open a browser.
download a driver based on your browser in your case it's Chrome from here: https://chromedriver.chromium.org/downloads
then driver = webdriver.Chrome(here you have to write the path of the driver),you can put it wherever you want.

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

Selenium Chrome Extension Interaction

I can load a Selenium Chromedriver extension in python. But I need to login in this extension in order to be able to use it. My question is how can I interact with this extension in order to login within it? The extension namely is the "Hoxx VPN".
Until I have the following code:
chop = webdriver.ChromeOptions()
chop.add_extension("D:/01_PhD/Fogadas/chromeextension/2.2.2_0.crx")
driver = webdriver.Chrome(chrome_options=chop)
Selenium Webdriver can interact with web pages only. Previously I have also tried it but unable to succeed.
See this as reference:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7805
You can use the Opera Webbrowser it's latest version i.e. 70+ having the in-built vpn which can be activated easily by selenium
Github have the required code to operate opera using selenium

Selenium Python script

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

Categories

Resources