Avoid opening browser on remote server during selenium call - selenium-webdriver

I have written a selenium application using webdriver. I wish to run it on a remote server. When I do that by logging into the server via putty (along with Xming), the selenium tries opening the browser on the server only and load the pages through the external display. However in doing that, it takes a lot of time than if I would have been able to get the browser open on my localhost only (and not the server). Is it possible for such thing to happen or opening on the server only is the only option (which is painfully slow). Kindly tell me if I am missing something as well.
Thanks in advance.

Try using Selenium Grid, instead of Putty, to run your Selenium application on a remote server. The Selenium website has an excellent Quick Start guide for using the Selenium Grid: http://code.google.com/p/selenium/wiki/Grid2.

You can run Selenium with a"headless" driver, HtmlUnitDriver, that does not actually open a browser:
http://code.google.com/p/selenium/wiki/HtmlUnitDriver
Note: HtmlUnitDriver will accept an argument, so that it can emulate a specific driver.

#Lori
I implemented the code but it still tries opening it from putty so takes a lot of time to get the work done. The code is as follows: 'code'
import sys
from scrapy.spider import BaseSpider
from scrapy.http import FormRequest
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from scrapy.http import Request
from selenium import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class DmozSpider(BaseSpider):
name = "linkedin_crawler"
#defence news
global company
global query
companyFilename = '<filename>'
f=open(companyFilename,"r")
f.seek(0)
company = f.readline().strip()
f.close()
queryFilename = '/var/www/Symantec/recon/recon/' +company+ '/Spider/LinkedIn/query.txt'
f = open(queryFilename)
f.seek(0)
query=f.readline().strip()
f.close()
start_urls = ['https://www.linkedin.com/uas/login'];
def __init__(self):
BaseSpider.__init__(self)
capabilities = webdriver.DesiredCapabilities()
self.selenium = webdriver.Remote(command_executor = 'http://localhost:5000/wd/hub', desired_capabilities = capabilities.FIREFOX)
def __del__(self):
self.selenium.quit()
def parse(self, response):
sel= self.selenium
sel.get(response.url)
global query
elem1 = sel.find_element_by_name("session_key")
elem2 = sel.find_element_by_name("session_password")
elem1.send_keys("myemailid")
elem2.send_keys("mypassword")
elem2.send_keys(Keys.RETURN)
return Request(query, callback=self.page_parse)
def page_parse(self,response):
global query
global company
sel= self.selenium
sel.get(query)
for i in xrange(10):
#for i in xrange(5):
nameFilename = ''
#print hxs
nlist = sel.find_elements_by_xpath('//ol[#class="search-results"]/li/div/h3/a')
fh = open(nameFilename,"a")
for j in xrange(len(nlist)):
url = nlist[j].get_attribute("href").encode('utf-8')
name = nlist[j].text.encode('utf-8')
fh.write(name)
fh.write("<next>")
fh.write(url)
fh.write('\n')
fh.close()
next = sel.find_elements_by_xpath('//a[#class="page-link"]')
next[0].click()
time.sleep(5)
To tun this script on server, I am using putty to fire the command. But then it again uses Xming to open the browser which makes the process slow again. So, how to run the script without opening the browser on my local machine via Xming so that this does not become the bottleneck. Thanks

Related

Selenium Webdriver - Keys not being sent to search boxes

I am starting to use Selenium Webdriver (Chrome) and while pages open automatically keys are not being sent for some reason. For example, I open google.com, and I use a "BY.ID" to aim at the search box by it's ID and send "Hi" but for some reason, it doesn't send the keys, I tried looking at the Selenium documentation but It kinda left me more confused, anybody knows why keys aren't being sent? Thanks in advance!
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome('/Users/thras/Desktop/Chromedriver/chromedriver')
browser.get('https://www.youtube.com')
title = browser.title
browser.implicitly_wait(3)
text_box = browser.find_element(By.ID,"search").send_keys('Hi')
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome('/Users/thras/Desktop/Chromedriver/chromedriver')
browser.get('https://www.google.com')
title = browser.title
browser.implicitly_wait(3)
text_box = browser.find_element(By.NAME, value="q")
text_box.send_keys('Hiii')
fixed, it was syntax issue it seems, I needed to add "value" before the actual ID

Jmeter WebDriverSampler fail with Chromedriver headless

I have some tests with WebDriverSampler in Jmeter that work correctly with chromedriver. It is a selenium script that opens a web page and checks that it contains a series of elements. Everything works right until I've tried with the chromedriver headless option.
In this case I get the exception "Expected condition failed: waiting for presence of element located by: By.xpath: ..." as if that element did not exist yet to be loaded. I do not know what can happen, because if I stop using the headless option, if everything works correctly and find the element that really exists.
This is an example of code used(it works without the headless option):
var wait = new support_ui.WebDriverWait(WDS.browser, 30);
var conditions = org.openqa.selenium.support.ui.ExpectedConditions
WDS.sampleResult.sampleStart();
WDS.sampleResult.getLatency();
WDS.browser.get('http://mi-app/');
try{
wait.until(conditions.presenceOfElementLocated(pkg.By.xpath('/ruta_de elemento_existente')));
WDS.log.info('OK')
}catch(e){
WDS.sampleResult.setSuccessful(false);
WDS.sampleResult.setResponseMessage('Fail');
WDS.log.error(e.message)
}
try{
wait.until(conditions.presenceOfElementLocated(pkg.By.xpath('/ruta_de elemento2_existente')));
WDS.log.info('OK2')
}catch(e){
WDS.sampleResult.setSuccessful(false);
WDS.sampleResult.setResponseMessage('Fail2');
WDS.log.error(e.message)
}
WDS.sampleResult.sampleEnd();
I hope someone can help me with this problem, because I need to use the headless option. Thank you very much for your time.
You can print the page source to jmeter.log file by using the following function:
WDS.log.info(WDS.browser.getPageSource())
Or even save it into a separate file like:
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File('test.html'), WDS.browser.getPageSource())
Or take screenshot on failure like:
WDS.browser.getScreenshotAs(org.openqa.selenium.OutputType.FILE).renameTo(new java.io.File('test.png'))
Check out The WebDriver Sampler: Your Top 10 Questions Answered article for more information.
Also be aware that if the machine where you run your Selenium tests doesn't have GUI you can still normally launch browsers using i.e. Xvfb on Linux or under Local System account on Windows

Browser opens many times while Geb testing

I wrote the following test for my simple Spring Boot Web application:
#SpringApplicationConfiguration(classes=[PDK])
#WebIntegrationTest
#DirtiesContext
class GebMainpageSpec extends GebSpec {
#Autowired
WebApplicationContext context;
def setup() {
System.setProperty("webdriver.chrome.driver", "chromedriver/win32/chromedriver.exe");
browser.driver = new ChromeDriver();
browser.baseUrl = "http://localhost:8080/";
}
def 'Static page present and works, check without pages'() {
when:
go ""
then:
assert title == "MyApp"
}
def 'Static page present and works, check WITH pages'() {
when:
to Mainpage
then:
LoginWithFormUsername.value() == "root"
}
}
These tests are apparently work, i.e. they pass or fail depending on page data.
The problem is that it opens TWO instances of Chrome browser to operate (by number of tests).
How to prevent that? May be reuse browser? Or may be close it after each test?
UPDATE
If I add something like below
def cleanupSpec() {
browser.driver.quit()
}
then all my tests startin to run twice, moreover, each second run they try with HtmlUnit (i.e. with "in memory" web browser, not Chrome).
You shouldn't instantiate the driver yourself when using GebSpec because it already handles the lifecycle of a driver instance through a browser instance it lazy initializes in it's getBrowser() method.
See the sections of The Book of Geb about how Geb interacts with WebDriver instances and about configuring the driver to be used via the config script to learn more.

JDBC DriverManager trying to use non specified Oracle driver

I am trying to connect to an Oracle Database using the java.sql.DriverManager in a JSF application. I am using a Tomcat v7 with ojdbc5.jar.
I have a very simple sample project that consists of nothing else than this piece of java code:
String url = "jdbc:oracle:thin:#DBSERV:DBPORT:DBSID";
String user = "account_admin";
String password = "my_assword";
Connection connection = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(url, user, password);
connection.close();
} catch ..
...
Executed I get the following error:
java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.driver.OracleDriver
Infact the class specified there "oracle.jdbc.driver.OracleDriver" is deprecated...and I can't change the Tomcat configuration. Therefore I specified "oracle.jdbc.OracleDriver" which loads just fine.
So the question is: Why does the DriverManager tries to load the "wrong" oracle driver, although I am loading another one?
I also tried as an alternative to Class.forName the following:
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
That does not change anything though. I also checked for the registered drivers in the following way:
Enumeration<Driver> driverList = DriverManager.getDrivers();
while(driverList.hasMoreElements()){
Driver driver = driverList.nextElement();
System.out.println(driver.getClass().toString());
}
The output:
class sun.jdbc.odbc.JdbcOdbcDriver
class oracle.jdbc.OracleDriver
So the desired driver seems to be registered, no trace of the deprecated "oracle.jdbc.driver.OracleDriver".
Thank you for any help
My problem just disappeared when I restarted the container. I can't explain why that is though.

Selenium hangs in loop

I'm using Selenium's Python bindings for a quick scraping job, but have found that for whatever reason, the Firefox WebDriver becomes unresponsive after precisely nine iterations.
Basic operation consists of loading a page, selecting a state from a dropdown menu of all fifty, clicking through to the results page and then returning to select another state. Irrespective of where I start in the list of states, after iterating through nine pages, the Firefox WebDriver becomes unresponsive, though no errors are thrown.
Code in question below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def set_up(url):
driver = webdriver.Firefox()
driver.get(url)
return(driver)
def search(driver):
for i in range(1, 49):
element = driver.find_element_by_id("StateList")
all_options = element.find_elements_by_tag_name("option")
print("Acquiring data for" + str(all_options[i].get_attribute("label")))
all_options[i].click()
driver.find_element_by_id("SearchButton").click()
#scrape page components here
driver.back()
url = 'http://www.example.com/'
driver = set_up(url)
search(driver)
Any thoughts?

Resources