Web Scraping: How to click button in selenium without opening webdriver? - python

I am using selenium scraping code in Windows VPS, Now code open chrome browser in order to click button, but I need to use the code in Ubuntu VPS.
As you know Ubuntu VPS don't provide any UI. So I need to click button in selenium without opening webdriver - chrome browser.
I tested --headless option but not works.
Follow my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
# chrome_options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options = chrome_options)
link = "site url"
driver.get(link)
button = driver.find_element(By.CSS_SELECTOR, "button")
button.click()
print("Clicked Accept_BTN")

Related

Why isn't this Selenium opening the website I'm requesting?

import time
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\Owner\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"--profile-directory=Profile 11")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('https://www.facebook.com')
time.sleep(25)
driver.quit()
I'm attempting to write a script that opens google, picks a Chrome profile, and goes to a website. The code above opens google and signs into the profile but it does not go to Facebook. Does anyone have any idea why?

Google chrome closes automatically after executing Python selenium code despite using the option class to keep it open

I have checked online and it was mentioned that Selenium closes the browser after running unless you use the option module or the driver.quit() or driver.close() functions but I used the option as shown in the code below but Chrome still closes after 2-3 seconds.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver_path = r"C:\Development\chromedriver.exe"
serv = Service(chrome_driver_path)
driver = webdriver.Chrome(service=serv, options=chrome_options)
driver.get("https://www.google.com")

CTRL S to save a chrome page contents using selenium python not working

I am trying to use CTRL + S in selenium to save contents of a page but can't get anything happening. If I try to do it using my keyboard the save window opens.
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.maximize_window()
action_chains = ActionChains(driver)
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads, download.prompt_for_download=False")
driver = webdriver.Chrome(options=options)
driver.get("https://imagecyborg.com/")
action_chains.send_keys(Keys.CONTROL).send_keys("s").perform()
The only thing that worked for me was pyautogui:
import pyautogui
pyautogui.hotkey('ctrl','s')
pyautogui.press('enter')

Using selenium: How to keep logged in after closing Driver in Python

I want to get my Whatsapp web (web.whatsapp.com) logged in, at the second time opening the Whatsapp web on chrome driver. Following is my code based on Python need your help.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_path = r"chromedriver.exe"
options = Options();
options.add_argument("user-data-
dir=C:/Users/Username/AppData/Local/Google/Chrome/User Data");
#options.add_argument("--start-maximized");
driver = webdriver.Chrome(chrome_path,chrome_options=options);
#driver = webdriver.Chrome();
driver.get('https://web.whatsapp.com/')
I tried on my Mac, below code and it worked perfectly fine, I don't need to login again
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=/tmp/tarun")
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://web.whatsapp.com/')
driver.quit()
For window you can try changing the path as below
options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://web.whatsapp.com/')
driver.quit()
Here it is for Windows. Works perfect on Python 3.6

How can I get the page source, without showing the page opened, using selenium with chromedriver and python?

I'm using selenium with Chrome driver; How can I get the page source, without showing the page opened? What I should specify in webdriver.ChromeOptions()?
Here the code:
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("???")
bowser = webdriver.Chrome(chrome_options=chrome_options)
browser = webdriver.Chrome()
try:
browser.get("www.google.com")
html_content = browser.page_source
#do stuff
browser.quit()
except WebDriverException:
print "Invalid URL"
You should not use ChromeDriver but some headless Webdriver like HtmlUnitDriver, explained here
If you are adamant to use selenium, then you can use any of the headless browsers such as htmlunit driver.
Else you can can just send a get request on the URL and get the response text.
Selenium / Chrome has a headless option, which allows you to load webpages from code:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
browser = Chrome(options=chrome_options, executable_path='path_to_chromedriver')
browser.get('https://wwww.mywebsite.com')

Categories