So I am using the JavaScript implementation of Selenium, WebDriverJS. As with any web browser automation the biggest hurdle is getting the code slow down long enough for page elements to load. My solution is this:
for each element I want to interact with I have a code block like this
xpath = "//div[@id='gs_lc0']/input[@id='gbqfq']"
driver.wait(function(){
return waitForElement(xPath,driver);
});
try{
element = driver.findElement(webdriver.By.xpath(xPath));
}catch(e){
console.log("Wait Function False Positive")
}
element.sendKeys("Let Me Google That For You\n";
with this as the function repeated in the wait function
var waitForElement = function(path, driver){
console.log("try: " + path)
try{
driver.findElement(webdriver.By.xpath(path));
}catch (e){
console.log("FAILURE")
return false;
}
console.log("SUCCESS")
return true;
}
now this code will work sometimes but othertimes it won't. I suppect the wait function isn't working at all and I'm just getting lucky webpage load times. So to test this theory I added the try function to the code block I cant even get the "NoSuchElementError" to be caught. So the brunt of my question is if there is some other way to form the tryCatch function so these errors will be caught.
Also here is what the head of my code looks like if want a full reproduction
var webdriver = require('selenium-webdriver'), element
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('google.com');