is it possible to controll browse(any) using python , - python

i am trying to control my browser using python, what I need is I give commands in terminal that should work on the browser like opening and searching for something(like scorling the bowser) and closing the browser
currently I am done with opening the browser and closing

yes, you can by using python selenium driver.
This is simple code to test. Also don't forget to download selenium driver.
https://chromedriver.chromium.org/downloads
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('./chromedriver')
driver.get("https://www.python.org")
print(driver.title)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
print(driver.current_url)
driver.close()
Read docs in link

This is achievable (at least in POSIX systems like Linux or BSD) by using pipes.

Related

How do I run Firefox browser with undetected_chromedriver in Python?

I'm trying to open a Firefox browser with undetected_chromedriver.
But only getting a default Firefox browser instead of getting the url I provided.
What did I miss or do wrong?
Here is the code I made so far.
import undetected_chromedriver as uc
import time
if __name__ == '__main__':
driver_path = "C:/Users/jay/Desktop/py/geckodriver.exe"
Firefox_path = "C:/Program Files/Mozilla Firefox/firefox.exe"
option = uc.ChromeOptions()
option.binary_location = Firefox_path
driver = uc.Chrome(executable_path=driver_path, options=option)
driver.get('https://google.com')
time.sleep(10)
I'd appreciate it if you could help me with this.
undetected_chromedriver is ONLY for chromedriver.
It modifies values directly inside binary file chromedrive.exe and it doesn't know how to modify values inside file geckodriver.exe.
See also repo GitHub - undetected-chromedriver.
There is:
"Works ... on .... Chromium based browsers".
"Automatically downloads the driver binary and patches it."
It means it automatically uses chromedrive.exe and it can't even use geckodriver.exe. And chromedrive.exe doesn't know how to communicate with Firefox - so it can't open page.
You can use it only with browsers which use engine Chromium - like Brave and maybe Opera, Microsoft Edge (but I didn't test it).

How do I use a feature on a website using python

I am trying to figure out how to activate/click on a feature using python. Like it goes to a page and click on a certain button. How can I do this? Are there any modules that may help?
Try using the selenium package in Python.
Once you pip install selenium and download chromedriver, you should be able to use something like this -
from selenium import webdriver
url = "your_url"
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome("/path/to/chromedriver", chrome_options=chrome_options)
driver.delete_all_cookies()
driver.get(url)
And after your page opens, you'll first have to find the element using inspect and then based on its name/id/class/etc, you can click on it using -
driver.find_element_by_name('<element_name>').click()

Selenium web driver Firefox opening blank page

I'd like to ask something about Selenium library in Python.
I'm trying to open a webpage, directly log onto it, and access another webpage behind it (I wanted to navigate on the website after the login) with a Python script. I've found the following code on the Internet but I have a problem with the line:
browser = webdriver.Firefox()
It just opens a blank page in Firefox and it looks like the script get stuck with it and does nothing afterwards. I tried in the Python interpreter and it's the same, it opens a blank page in Firefox and I lose the hand (I can't enter other commands).
python interpreter blocked:
I'm using Selenium-3.3.1 and I work under CentOS 6.5.
Is it normal? Am I missing something obvious?
Here is my code:
#!usr/bash/python
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
def loadedPage(browser):
return browser.find_element_by_tag_name("body") != None
browser = webdriver.Firefox() #supposedly just a firefox webdrive instance creation
browser.get("http://machine/machineDir/index.php")
wait = ui.WebDriverWait(browser, 10)
wait.until(loadedPage)
username=browser.find_element_by_id("username")
username.send_keys("userTest")
passwd=browser.find_element_by_id("password")
passwd.send_keys("userTestpass")
passwd.send_keys(Keys.RETURN)
As you are using selenium 3, firefox browser can't be instantiate directly, you need to configure gecko driver for the same.
System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
I fixed it using the right version of Selenium for my old Firefox.
Firefox version: 17.0.10
Selenium version installed: 2.40

Force Selenium Chrome Driver to use QUIC instead of TCP

I am working on downloading HAR from Chrome for YouTube through Selenium Python Script.
Code Snippet:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
chrome_options.add_argument("--enable-quic")
self.driver = webdriver.Chrome(chromedriver,chrome_options = chrome_options)
self.proxy.new_har(args['url'], options={'captureHeaders': True})
self.driver.get(args['url'])
result = json.dumps(self.proxy.har, ensure_ascii=False)
I want QUIC to be used whenever I download HAR but when I look at the packets through Wireshark Selenium driver is using TCP only. Is there a way to force Chrome Driver to use QUIC? Or Is there an alternate to BMP?
A similar thing has been asked for Firefox in this question How to capture all requests made by page in webdriver? Is there any alternative to Browsermob? and there was a solution with Selenium alone without need of any BMP. So is it possible for Chrome?
Workaround for this problem could be: start Chrome normally (with your default profile or create another profile) and enable quic manually. Then start chromedriver with your profile loaded.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/home/user/.config/google-chrome")
driver = webdriver.Chrome(executable_path="/home/user/Downloads/chromedriver", chrome_options=options)

Using Selenium in the background

I'm using Selenium and chrome webdriver but when I run scripts it opens a window. Is there any way that it can access the internet without the window popping up?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://ps.rsd.edu/public/")
elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)
driver.quit()
For example, this goes to my school's grade site and puts in a username and password but I want to do this without the browser popping up if that's possible.
I would suggest try using headless PhantomJs GhostDriver (which is a relatively new thing). As this is the native Selenium Webdriver way of doing it.
Download PhantomJs executables from http://phantomjs.org/download.html.
driver = webdriver.PhantomJS("./phantomjs") # path to phantomjs binary
driver.get("https://ps.rsd.edu/public/")
elem = driver.find_element_by_name("account")
elem.send_keys("Username")
elem2 = driver.find_element_by_name("pw")
elem2.send_keys("Password")
elem.send_keys(Keys.RETURN)
driver.quit()

Categories