How to disable navigator.webdriver using Selenium in VBA? [duplicate] - vba

Been searching for a while and tried all the solutions present but none appear to be working. I created a "slide show" that will first log in, then alternate between tabs. All of that is working but i cannot get rid of the
"Chrome is being controlled by automated test software" bar. Any advise?
Code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
usernameStr = 'test'
passwordStr = 'test'
browser = webdriver.Chrome()
#first tab
browser.get(('www.testwebsite.com?'))
# fill in username and hit the next button
username = browser.find_element_by_id('username')
username.send_keys(usernameStr)
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'password')))
password.send_keys(passwordStr)
nextButton = browser.find_element_by_class_name('emp-submit')
nextButton.click()
#second tab
browser.execute_script("window.open('about:blank', 'tab2');")
browser.switch_to.window("tab2")
browser.get('www.testwebsite.com')

Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option("excludeSwitches",["enable-automation"])
driver_path = '/Users/myuser/Downloads/chromedriver'
driver = webdriver.Chrome(executable_path=driver_path, chrome_options=options)
driver.get('https://google.com')
driver.close()

When you open Chrome Browser in through ChromeDriver this infobar containing the notification is embedded as follows:
Chrome is being controlled by automated test software
Browser snapshot without the argument disable-infobars:
But if you add the argument disable-infobars through an instance of ChromeOptions you can get rid of this infobar as follows:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.google.com/')
Browser snapshot applying the argument disable-infobars:

THIS IS WORKING WITH LATEST RELEASE.
Try it by changing driver path under "service_obj"
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
service_obj = Service(r"C:\Users\Documents\Sublime_srk\drivers\chromedriver_win32\chromedriver.exe")
driver = webdriver.Chrome(options=chrome_options,service=service_obj)
driver.get("https://www.google.co.in/")

Click on the "x" to close the bar. It doesn't work initially, but then maximize and restore the window and it should disappear. This is for anyone who doesn't trigger their tests through Java.

Related

Trying to open url in google chrome, but closes immediately after being launched with selenium

I tried to launch chrome with selenium. but as soon as the browser loads the urls, google chrome closes automatically. here is the code:
'''
from selenium import webdriver
url= 'https://www.gmail.com'
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(url)
'''
Because selenium4 no need to use driver.close()
method they automatically close the driver after execution. So options.add_experimental_option("detach", True) argument will help you to prevent to close immediately after once launch the url with google chrome
# selenium 4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url= 'https://www.gmail.com'
driver.get(url)
time.sleep(2)

Unable to click link with Selenium in Python

I am working on a web scraping project at the moment. The website I am trying to get data from is not the easiest to work with. I am using Selenium, and have worked my way through most of the items I want to select.
The website: http://crashinformationky.org/AdvancedSearch
I can get the website to open, and select different properties. But when I select Data, then try and "click" on Today, to change the date. Nothing works.
I have tried using the xpath, css selectore, link text, partial link text, nothing works.
Here is my most recent attempt.
WebDriverWait(driver, timeout=5).until(driver.find_element(By.CSS_SELECTOR, '#QueryPanel-cond-2 > div:nth-child(4) > a'))
date_one = driver.find_element(By.CSS_SELECTOR, '#QueryPanel-cond-2 > div:nth-child(4) > a')
date_one.click()
date_one_enter = driver.find_element(By.XPATH,'//*[#id="dp1647307835636"]')
date_one_enter.send_keys('01/01/2016')
Welcome Kyle!
As a personal preference I like to use Full XPATH's when selecting items with Selenium. I was able to click on TODAY, clear the field, send a new date and hit ENTER. See below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--enable-logging')
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
url = "http://crashinformationky.org/AdvancedSearch"
driver.get(url)
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[3]/a"))).click()
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[10]/div/div[3]"))).click()
WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/div/div[3]/a"))).click()
field = driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/form/div[2]/div/div[1]/div[2]/div[2]/div/div[2]/div[2]/div/div[3]/input")
field.send_keys(Keys.COMMAND + "a")
field.send_keys(Keys.DELETE)
field.send_keys('01/01/2016')
field.send_keys(Keys.ENTER)
Do note that I am using a MAC, so when I send the Keys.COMMAND that would be Keys.CONTROL for Windows.

Trouble clicking on a next-page button with webscraping (Selenium)

I'm trying to webscrape this page https://www.bumeran.com.pe/empleos-publicacion-menor-a-7-dias.html. However, I'm having trouble clicking on the next-page button to scrape all the pages. I've tried doing this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
PATH = "C:\Program Files (x86)\chromedriver.exe"
wd = webdriver.Chrome(PATH, options=options)
wd.maximize_window()
wd.get("https://www.bumeran.com.pe/empleos-publicacion-menor-a-7-dias.html")
def pag_sig():
element_present = EC.visibility_of_element_located((By.XPATH, '//*[#class="Pagination__NextPage-sc-el3mid-4 gSlsBf"]/i'))
boton = WebDriverWait(wd,5).until(element_present)
boton.click()
page_max=127 #I get this with another piece of code
for i in range(page_max):
pag_sig()
However, this does not work properly. Sometimes my scraper clicks on other links rather than the button itself. I've tried adding waits but it doesn't work. How could I approach this problem?
May its because of not scrolling down. Try the below xpath and code once.
#xpath - //div[#id='listado-avisos']//button[#class='Pagination__NextPage-sc-el3mid-4 gSlsBf']
driver.get("https://www.bumeran.com.pe/empleos-publicacion-menor-a-7-dias.html")
for i in range (5):
nextoption = driver.find_element_by_xpath("//div[#id='listado-avisos']//button[#class='Pagination__NextPage-sc-el3mid-4 gSlsBf']")
driver.execute_script("arguments[0].scrollIntoView(true);", nextoption)
driver.execute_script("window.scrollBy(0,-300)")
nextoption.click()
time.sleep(3)
You can click on the button instead of directly clicking on the i below can help, I hope
button = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='Pagination__NextPage-sc-el3mid-4 gSlsBf']")))
button.click()

How I can run selenium python with headless browser

I have using python selenium to do some test but I don't want it to open any browser, so how I can use a headless browser?? can you guys help me :((
Here is my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver')
url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)
driver.implicitly_wait(15)
driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()
Just set headless to True:
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
Full code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome('/Users/toanhac/Downloads/chromedriver', options=options)
url = 'https://mail.google.com/mail/u/0/#inbox'
driver.get(url)
driver.implicitly_wait(15)
driver.find_element_by_id("identifierId").send_keys(mail)
driver.find_element_by_id("identifierNext").click()
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_id("passwordNext").click()

splinter: how to add chrome options?

I'm using splinter(v0.7.3) for web testing under linux, while on chrome, the default sample code can not running:
from splinter import Browser
from pyvirtualdisplay import Display
d = Display(visible=0, size=(800, 600))
d.start()
b = Browser('chrome')
b.visit('http://www.google.com')
b.quit()
d.stop()
While running, I got the exception like this:
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
And I test the same function in selenium with some chrome option added:
from selenium import web driver
from selenium.webdriver.chrome.options import Options
from pyvirtualdisplay import Display
d = Display(visible=0, size=(800, 600))
d.start()
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
b = webdriver.Chrome(chrome_options=opt)
b.get('http://www.google.com')
b.quit()
d.stop()
This works ok, the difference is the --disable-setuid-sandbox option added to chrome driver, if the option not add, there would be a zombie chrome-sandbox process under chromium-browser.
The problem here is, I don't know how to pass a chrome.options.Option instance to splinter.Browser(), I browsed the implementation under splinter/driver/webdriver/chrome.py, it seems that there is no entry to pass such a instance to splinter.Browser(). Is there some other way to pass options to chrome driver?
Create a new instance of BaseWebDriver and set .driver with an instance of the Chrome driver. This example starts Chrome maximized:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from splinter.driver.webdriver import BaseWebDriver, WebDriverElement
options = Options()
options.add_argument('--start-maximized')
browser = BaseWebDriver()
browser.driver = Chrome(chrome_options=options)
browser.visit('https://www.google.com')
The only way I could ever do this was by using the add_argument method with selenium.webdriver.ChromeOptions like so:
from selenium.webdriver import ChromeOptions
from splinter import Browser
chrome_options = ChromeOptions()
chrome_options.add_argument(your_argument)
b=Browser("chrome", options=chrome_options)
b.visit('http://www.google.com')
b.quit()
so in your code would be:
from splinter import Browser
from selenium.webdriver import ChromeOptions
from pyvirtualdisplay import Display #I'm not certain what this is...
d = Display(visible=0, size=(800, 600))
d.start()
chrome_options = ChromeOptions()
chrome_options.add_argument('disable-setuid-sandbox')
b = Browser('chrome')
b.visit('http://www.google.com')
b.quit()
d.stop()
Note: I was unable to test this with your argument specifically because I recently broke my GRUB so I am stuck in windows, and the disable-setuid-sandbox option is linux-only. However, I have been using this method with the headless argument for a while.
I am not 100% sure that this will work but I just looked at the docs for splinter and it says.
You can also pass additional arguments that correspond to Selenium DesiredCapabilities arguments.
Looking into the sourcecode of Splinter calling Browser can take some arguments. These arguments will then be passed to create an Instance of the Chrome WebDriver. So I went to the selenium sourcecode and saw the constructor looks like this:
def __init__(self, executable_path="chromedriver", port=0,
chrome_options=None, service_args=None,
desired_capabilities=None, service_log_path=None):
There is a parameter for chrome_options so it should be possible to pass it using this parameter. So if I'm correct this should work fine:
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
b = Browser(browser='chrome', chrome_options=opt)
Edit
Alternatively you could pass the options as desired capabilities aswell:
opt = Options()
opt.add_argument('--disable-setuid-sandbox')
dc = opt.to_capabilities()
b = Browser(browser='chrome', desired_capabilities=dc)
I've been working on a fork of splinter for the past couple weeks, you can check out my dev branch if you want. I have added this and other features.
Options can be passed as a list to the chrome_options parameter
from splinter import Browser
options = ['--start-maximized', '--disable-setuid-sandbox']
with Browser('chrome', chrome_options=options) as browser:
browser.visit('http://www.google.com')
Edit:
So it turns out this was possible with splinter all along just using **kwargs which passes the various available options to the selenium driver(s). For example:
from splinter import Browser
options = {'chrome_options':['--start-maximized', '--disable-setuid-sandbox']}
with Browser('chrome', **options) as browser:
browser.visit('http://www.google.com')