Does the Selenium clickAt command work with headless browsers? - selenium

I'm considering to replace Firefox with a headless browser such as PhantomJS (or htmlunit) for web scraping activities with selenium.
I think the clickAt command in selenium depends on page graphic rendering, and it won't work in PhantomJS. Can anybody confirm this?

This worked for me (in Python 3.5):
from selenium import webdriver
import time
if __name__ == '__main__':
driver = webdriver.PhantomJS('<YourPathtoPhantom>')
driver.set_window_size(1400, 1000)
driver.get("http://www.google.com")
el = driver.find_element_by_name('btnI')
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 5, 5)
action.click()
action.perform()
time.sleep(2)
driver.save_screenshot('screen.png')

Related

Can I install chromedriver on python anywhere and not use it headless?

I am trying to use this code that works on my local machine on python anywhere and i want to understand if it is even possible:
from selenium import webdriver
from bs4 import BeautifulSoup
import time
# Initialize webdriver
driver = webdriver.Chrome(executable_path="/Users/matteo/Downloads/chromedriver")
# Navigate to website
driver.get("https://apnews.com/article/prince-harry-book-meghan-royals-4141be64bcd1521d1d5cf0f9b65e20b5")
time.sleep(5)
# Parse page source
soup = BeautifulSoup(driver.page_source, "html.parser")
# Find desired elements using Beautiful Soup
elements = soup.find_all("p")
# Print element text
for element in elements:
print(element.text)
# Close webdriver
driver.quit()
Do i need to have installed chrome to make that work or is chromium enough? Because when i run that code on my local machine a chrome page opens up. How does that work on python anywhere? Would it crush?
I am wondering if the code i am using only works if someone is on a GUI with Chrome installed or if it can work on python anywhere too.
The short answer is no. ChromeDriver must have chrome installed. You can run your tests headless for time save, but chrome still must be installed.

Selenium code not executed after driver.get (browser is IE)

I am new to selenium. My code works fine with Chrome webdrive. However, when I switch to IE webdrive, my code does not execute after drive.get.
from selenium import webdriver
driver = webdriver.Ie(executable_path=r"C:\Drivers\IE_Driver\IEDriverServer.exe")
driver.get("https://www.amazon.com")
print(driver.current_url)
print ("Test")
I have verified your code & its working without any issue.
from selenium import webdriver
driver = webdriver.Ie(executable_path=r"C:\IEDriverServer.exe")
driver.get("https://www.amazon.com")
print(driver.current_url)
print ("Test")
Output:
I would suggest to Download IE Drivers based on your OS (Windows 32 or 64 bit) and then try once again
https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#required-configuration
you have to do the prerequisite steps mentioned there , expecially disabling the enhanced protection. I was able to reproduce your issue.
uncheck the box for everything internet,local , trustred and restricted
I have tried set webdriver.IeOptions ignore_protected_mode_settings = True, then drive.get response.

Click on elements in Chrome Extension with selenium

I have been searching on the internet using Selenium (Java) interacting with Google Chrome Extension but have not been able to find an answer.
First Question
Is there a way to launch the chrome extension since Selenium only interact with WebView but not on the chrome extensions button in the browser ?
I try this method
"chrome-extension://id/index.html" but the extension did not launch as expected. I like find if there is another way to launch a chrome extension through selenium
Second Question
I am trying to click on the elements in a chrome extension with Selenium webdriver. How do I do it ? I tried the driver.CurrentWindowHandle , but it does not detect the chrome extension.
Thanks
Below is the solution with pyautogui (similar to autoit in java - so you can extend the same solution for java also).
Pre-Condition:
save the extension image in the project folder (I saved it under "autogui_ref_snaps" folder in my example with "capture_full_screenshot.png" name
Python:
Imports needed
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from Common_Methods.GenericMethods import *
import pyautogui #<== need this to click on extension
Script:
options = ChromeOptions()
options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
If you are loading an extension and it's not available in incognito mode then follow my answer in here to enable it.
Try to click on extension with this JsExecutor method:
driver.execute_script("window.postMessage('clicked_browser_action', '*')")

Selenium Headless with Service

I know you can run Selenium headless alongside WebDriver, but is there a way to do so with the service? I'm trying the following and it just opens the browser normally, seemingly ignoring Xvfb. This is on a Mac if that happens to matter.
from pyvirtualdisplay import Display
import selenium.webdriver as webdriver;
import selenium.webdriver.chrome.service as service
# ...
self.display = Display(visible=0, size=(1024, 768))
self.display.start()
self.service = service.Service('/path/to/chromedriver');
self.service.start();
# Various Chrome option stuff clipped
browser = webdriver.Remote(self.service.service_url, desired_capabilities=options.to_capabilities());
Fyi -- not a real solution, but for the time being, I'm using Chrome's window-size and window-position flags to keep selenium out of the way e.g.,
options = webdriver.ChromeOptions();
options.add_argument('--window-size=100,100')
options.add_argument('--window-position=100,1200')
browser = webdriver.Remote(service_url, desired_capabilities=options.to_capabilities())
Maybe, this is what you want: generalredneck/headless-selenium

Is there selenium2 Chrome driver for Ruby/Python?

I want to use Selenium2 Chrome driver with Ruby/Python, but I only found those for Java and .NET on http://code.google.com/p/selenium/downloads/list . Could someone tell me is it possible to drive Chrome with Python or Ruby in Selenium2? Thanks in advance!
For python you have to download the chrome driver from http://code.google.com/p/chromium/downloads/list. Extract the zip file and set the chrome driver path in Path environment variable. Please see the below example using python -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)
assert "Google" in driver.title
driver.quit()
There definitely is for Python, you have to install the chrome driver, get it here for your appropriate OS: http://code.google.com/p/chromium/downloads/list. to start up a chrome instance do this:
self.webdriver = webdriver.Chrome(executable_path=[path to chrome driver])