Way to download and install Chrome by script? Python, for Mac - python

I have a task, and a part of it is to download and install latest Chrome (like step to test something).
And I can't get how I can do it by script (Python). I can download it, but after it I have to execute a lot of actions, like path to install, clicking 'next' in installing window etc.
Maybe, someone knows how can I do it?
Sorry, for stupid question, I am new in programming.
Script for download Chrome:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.google.com/chrome/?brand=CHBD&gclid=EAIaIQobChMI8ob1rLv26AIVw7LVCh1vgQ5XEAAYASAAEgLHsfD_BwE&gclsrc=aw.ds")
time.sleep(5)
element = driver.find_element_by_id("js-download-hero")
element.click()
time.sleep(20)

Related

Selenium opening Brave, but not going to URL. Using Python

Yes, yes, I know everyone is going to be angry at me for asking this question again. But I have looked at every question (related to Brave, Selenium, Python and Linux) in Stack Overflow, but nothing has worked for me. Here is my problem: I run the code. No errors. Brave opens up and says it is being controlled by “automated test software” (good sign). But then it doesn't go to the URL I want it to go to. I have checked the chromedriver and Chromium version, they match. I also use Ubuntu 20.04 LTS. Last thing, I installed Brave using snap.
Here is my code:
from selenium import webdriver
brave_path = "/snap/bin/brave"
option = webdriver.ChromeOptions()
option.binary_location = brave_path
driver = webdriver.Chrome("/bin/chromedriver_linux64/chromedriver", options=option)
driver.get("https://youtube.com/")

Selenium Opens Browser But Refuses to Execute the Next Line of Code

It looks like after opening the browser, Selenium cannot move on for some reasons I can't figure out. No error was ever displayed.
Here is my simple code:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Using Chrome to access web
browser = webdriver.Chrome(executable_path = "C:\Program Files\Google\Chrome\Application\chrome.exe")
print ("done")
# Open YouTube website
browser.get("https://www.youtube.com/")
The browser opens just fine, but the print("done") statement is never executed. (In the terminal the word "done" was never printed. So, it infers that the selenium has never finished executing the command to open the browser even though the browser has opened, and I have waited for several minutes.
Thanks in advance to our wonderful StackOverflow community!
The reason is because you are using chrome.exe which is for browser. Instead you should download chromdriver.exe, Please download from here. You should download Latest stable release: ChromeDriver 94.0.4606.61 (as on 3rd-oct-2021). Keep that in your automation directory and any directory of your preference.
driver_path = r'C:\\Users\\username\\Desktop\\Automation\\chromedriver.exe'
driver = webdriver.Chrome(executable_path = driver_path)
Note that, in place of driver_path, you should give the path where you've kept the chromdriver.exe
You should use chromedriver.exe instead of the path to your chrome.exe.
Download the chromedriver suitable for your chrome version, from here:
donwload Chromedriver.exe
Afterwards, do something like this:
browser = webdriver.Chrome("E:\YourPathToChromeDriver\chromedriver.exe")
Try these steps:
Check your Google Chrome version here "chrome://settings/help"
Download chromedriver.exe from "https://chromedriver.chromium.org/downloads"
Change executable path to newly downloaded file.
As mentioned above, this issue is the chrome.exe. You need to use a chromedriver instead. You can download one manually here https://chromedriver.chromium.org/downloads and then set the path to it like you did to the chrome.exe.
However, instead of downloading the chromedriver manually, I recommend using a library on GitHub which does that for you and loads it from cache if there is already one installed. (I'm not the owner nor the maintainer of this repository, but I do find it rather simple to use.)
https://github.com/SergeyPirogov/webdriver_manager
First you'd need to pip install webdriver-manager and then you can use it as following:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())

Running a selenium based exe to scrape data

What is working
I have created a Python file that opens a Firefox browser using Selenium and scrapes data to a excel file. I have also converted this file to exe. No errors were created during exe. This was done by Pyinstaller.
What is not working.
When I run the new created exe on official laptop it doesn't open a browser or give any output. Just a command window opens and closes immediately.
What is my intention.
My intention is to share this script with colleagues so that the data needed can be easily scraped to excel file on any PC and would be efficient process rather than looking up the same on website. I would not want to them to have any admin access to change anything on their PC. Also, I do not want them to have multiple browsers opening up on their PC.
Question
Do I need to install and provide geckodriver path on every PC? Perhaps this is the reason Firefox is not opening despite being available?
Code Below
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import pandas as pd
productName = []
number = []
divList = []
driver = webdriver.Firefox()
driver.get("website")
radioBtn = driver.find_element_by_id("ContentPlaceHolder1_company")
radioBtn.click()
element = driver.find_element_by_id("ContentPlaceHolder1_TextBoxInput1")
element.send_keys("CompanyName")
Search = driver.find_element_by_id("ContentPlaceHolder1_view1Continue")
Search.click()
driver.implicitly_wait(10)
driver.find_element_by_id("ctl00_ContentPlaceHolder1_92564").click()
driver.implicitly_wait(2)
divList = driver.find_elements_by_class_name('nopgbrk')
for div in divList:
number.append(div.find_element_by_xpath('a').text)
productName.append(div.find_element_by_xpath('span').text)
df = pd.DataFrame({'Product':productName, 'Registration': number})
df.to_excel("C:\Python\Scrapeddata\Scrape.xls")
I anticipate, the reason for this would be the incompatibility of chromedriver version and the chrome version of your official laptop.
In the development phase, the system on which it was being developed had chrome, whose version was compatible with selenium chromedriver version. So it ran and processed output successfully. But your official laptop did not have the same version that of your system's chrome, so it is not compatible.
Please check chrome version by going to settings -> About Chrome.
Download chromedriver for the same version which you checked above from here and then create exe if it supports:
https://sites.google.com/a/chromium.org/chromedriver/downloads
Or if you know the version of your chromedriver the easiest would be to uninstall chrome on your official laptop and install chrome of the same version that of chromedriver.

Message: 'chromedriver' executable needs to be in PATH

So, I am using OS X El Capitan with Eclipse (Neo) and Python. I am wrote some Python with Selenium scripts.
These scripts were working fine.
Then I upgraded from OSX Sierra to El Capitan
Note: this is about the only major change
that I did to my setup.
When I tried to run the same scripts, now, I get the
Message: chromedriver executable needs to be in PATH
As far I can tell chromedriver IS on my PATH.
Now, I can only make my scripts work if I hard copy the path to my chrome driver, which is not the most elegant way to do things, as we all know.
Has anyone else has/had this issue? Thoughts on any configuration with my Eclipse + Python.
My Eclipse and Python project screen shot
my PYTHONPATH screen shot
I dont know about python. But the implementation for the webdriver should be same irrespective of the language. Here you have called the chrome webdriver, but have you defined the path for chromedriver.exe in you code? For example, in java it looks like this
System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-3.4.0/chromedriver.exe");
driver = new ChromeDriver();
The error you are getting is referring to the chromedriver.exe not the python environment.
Python Solution from Google documentation:
import time
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver') # Optional argument, if not specified will search path.
driver.get('http://www.google.com/xhtml');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()
First of all verify your driver path , if you open a command prompt and type in chromedriver and hit Enter. If you get message "Starting ChromeDriver 2.15.322448 .." your path is correctly set up.
In python, alternate solution is you can use
driver = webdriver.Chrome("path_to_driver/chromedriver.exe")
Hope this should work !

Python test using selenium cannot perform simple test

I am trying to learn about Selenium but I am not able to get even a simple program to test. Selenium webdriver seems to not be cooperating with Firefox and I am very frustrated, so I come to Stack Overflow for help.
For background, I use Python, can install with pip, and know command line.
I am on windows 10, firefox 48, and selenium webdriver 3 with python 3.5.2
Whenever I run the selenium test, (it opens a Firefox windows and the selenium website)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.seleniumhq.org')
I always get an error:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\ ... \AppData\Local\Temp\tmp68m5rtwt If you specified a log_file in the FirefoxBinary constructor, check it for details
It also opens a firefox window and has the link of about:blank&utm_content=firstrun(not a valid url)
I have looked across the internet for a similar situation, but nothing really close. I also tried many tutorials and made sure that I installed selenium the right way. I noticed that firefox was recently updated, but I am not sure if this has any effect.
I would appreciate any help for this, and instructions for what I should do.
Firefox 48+ doesn't support webdriver.Firefox().
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps["marionette"] = True
caps["binary"] = "path/to/your/firefox"
browser = webdriver.Firefox(capabilities=caps)
browser.get('http://www.seleniumhq.org')
This is what I was trying
1. download geckodriver.https://github.com/mozilla/geckodriver/releases. v.0.10.0is for selenium 3(beta).
2. add PATH your geckodriver.
4. rename it to wires
5. restart shell
6. check the version
$ wires --version
7. and run above the code.

Categories

Resources