Selenium with web driver in Linux having proxy is a Bug or some work around is there?

38 views
Skip to first unread message

Sandeep Neema

unread,
Jan 13, 2011, 4:25:35 AM1/13/11
to Selenium Users, s.n...@tcs.com
The below is the test case which i am trying to run in Linux with the
help of eclipse:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Test1 {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the
interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();

// And now use this to visit Google
driver.get("http://www.google.com");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
element.sendKeys("Cheese!");

// Now submit the form. WebDriver will find the form for us
from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
}

After running i am getting the following error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Cannot find element with name: q
System info: os.name: 'Linux', os.arch: 'i386', os.version:
'2.6.29.4-167.fc11.i686.PAE', java.version: '1.6.0_0'
Driver info: driver.version: HtmlUnitDriver
at
org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:
545)
at org.openqa.selenium.By$4.findElement(By.java:148)
at
org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:
279)
at Test1.main(Test1.java:18)


Is this because my server is beyond the proxy or some settings have to
be done before using Web-Driver in Selenium?

BillR

unread,
Jan 13, 2011, 12:59:42 PM1/13/11
to Selenium Users
It might be worth trying the FF driver.
> org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDrive­r.java:

Sandeep Neema

unread,
Jan 14, 2011, 4:24:46 AM1/14/11
to Selenium Users
Hey BillR,

Can You help me, how i can successfully execute this using Firefox
driver.

I have writted this script with firefox driver in linux with proxy,
but still it is showing following error:


import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class GoogleSuggest {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
File a = new File("/home/sandeep/.mozilla/firefox/
jbqo270u.default");
FirefoxProfile profile= new FirefoxProfile(a);
String b = "/tmp/
customProfileDirc860a17656a74e64bd292e153ce54040/proxy.pac";
Proxy p = new Proxy();
p.setProxyAutoconfigUrl(b);
profile.setProxyPreferences(p);
WebDriver driver = new FirefoxDriver(profile);
// Go to the Google Suggest home page
driver.get("http://www.google.com/webhp?complete=1&hl=en");
// Enter the query string "Cheese"
WebElement query = driver.findElement(By.tagName("q"));
query.sendKeys("Cheese");
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
while (System.currentTimeMillis() < end) {
// Browsers which render content (such as Firefox and IE)
// return "RenderedWebElements"
RenderedWebElement resultsDiv = (RenderedWebElement)
driver.findElement(By.className("gac_m"));
// If results have been returned,
// the results are displayed in a drop down.
if (resultsDiv.isDisplayed()) {
break;
}
}
// And now list the suggestions
List<WebElement> allSuggestions =
driver.findElements(By.xpath("//td[@class='gac_c']"));
for (WebElement suggestion : allSuggestions) {
System.out.println(suggestion.getText());
}
}
}

Is there a way how can i pass username and password for the proxy?

BillR

unread,
Jan 14, 2011, 1:43:00 PM1/14/11
to Selenium Users
Hi Sandeep,

Are you in a part of the world where google.com redirects? I saw one
posting that this would be a problem and using the local google
address would work.

> driver.get("http://www.google.com/webhp?complete=1&hl=en");

Sorry, I don't have experience with proxy and webdriver.
Reply all
Reply to author
Forward
0 new messages