0

@Arran solved, selenium dont work well with
text() function, updated xpath to ://table[@width=200]/tbody/tr[1]/td/a

I need manipulate a internal system and it never get on time.
I changed timeouts to load and script to 30 hours to see its really a time problem.
I disabled javascript.
Nothing worked.
I created a very minimal version of my Selenium code and a minimal plain html file version that i put on my webserver.
When i open on Chrome and type $x('//table[@width=200]/tbody/tr[1]/td/a/text()') on console the xpath get what i want.
But when i do the same on PhantomJSDriver there is a neverendig process ( i know the timeout is set to 30 hours)
When i compare by Fiddler the content is the same.
I dont need GPU here but when i try the ChromeDriver its throw an exeption related with gpu:
[4588:4592:0519/143844:ERROR:gpu_info_collector_win.cc(140)] Could not read gaming score from assessment results.

The minimal html is:

<html>  
 <head>
    <title>Its a test</title>
 </head>
 <body>
    <table width=200>
        <tbody>
            <tr>
                <td>
                    <a href='http://uej65ge.com/lnk001.html'>text01</a>
                </td>
                <td>
                    <a href='http://uej65ge.com/lnk002.html'>text02</a>
                </td>               
            </tr>
            <tr>
                <td>
                    <a href='http://uej65ge.com/lnk003.html'>text03</a>
                </td>
                <td>
                    <a href='http://uej65ge.com/lnk004.html'>text04</a>
                </td>               
            </tr>
        </tbody>
    </table>
 </body>
</html> 

The WebDriver code is:
//-------------------------------------------------

public void start() throws FileNotFoundException {
        WebDriver driver;
        driver = getPhanthomDriver();
        scrapTest(driver);
    }

//-------------------------------------------------

private WebDriver getPhanthomDriver() {
        WebDriver driver = null;
        try {
            DesiredCapabilities caps = new DesiredCapabilities();                        
            String pathToPhantom = ConfigProvider.getStringConfig("pathToPhantom");                    
            caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, pathToPhantom);
            caps.setJavascriptEnabled(false);
            driver = new PhantomJSDriver(caps);            
        } catch (IOException ex) {
            ConsoleHelper.printMessage("ERRO_io:" + ex.getMessage(), new Date());
        } catch (Exception ex) {
            ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
        }
        return driver;
    }

//-------------------------------------------------

private WebDriver getChromeDriver() {
        // [4588:4592:0519/143844:ERROR:gpu_info_collector_win.cc(140)] Could not read gaming score from assessment results.
        // I dont need GPU here !!!
        WebDriver driver = null;
        try {            
            String pathToChrome = ConfigProvider.getStringConfig("pathToChrome");
            System.setProperty("webdriver.chrome.driver", pathToChrome);
            driver = new ChromeDriver();            
        } catch (Exception ex) {
            ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
        }
        return driver;
    }

//-------------------------------------------------

public void scrapTest(WebDriver driver) throws FileNotFoundException {        
        // this its form more than 05 minutes without return
        try {
            long pollingForSecond = 1;
            long timeOutInSeconds = 999999;
            if(driver == null){ throw new Exception("Erro, driver informado era nulo");}
            String initialUrl = "http://devw7lng:8080/CPS/xpts.html";
            driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.HOURS);
            driver.manage().timeouts().setScriptTimeout(30, TimeUnit.HOURS);
            driver.get(initialUrl);
            final String xpath_categoriasTexto = "'//table[@width=200]/tbody/tr[1]/td/a/text()'";
            List<WebElement> elements = null;
            try {

                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                        .withTimeout(timeOutInSeconds, TimeUnit.SECONDS)
                        .pollingEvery(pollingForSecond, TimeUnit.SECONDS)
                        .ignoring(NoSuchElementException.class)
                        .ignoring(org.apache.http.NoHttpResponseException.class);
                elements = wait.until(new Function<WebDriver, List<WebElement>>() {
                    @Override
                    public List<WebElement> apply(WebDriver driver) {
                        System.out.print(".");
                        return driver.findElements(By.xpath(xpath_categoriasTexto));
                    }
                });
            } catch (Exception err) {
            }
            boolean ok = false;
            ok = ((elements != null) && (elements.size() > 0));
            int x = 0;
        } catch (Exception ex) {
            ConsoleHelper.printMessage("ERRO_ex:" + ex.getMessage(), new Date());
        }
    }

//-------------------------------------------------

newway
  • 647
  • 1
  • 13
  • 21
  • There is someone using text() function with selenium here: http://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing –  May 20 '13 at 00:20

1 Answers1

2

I am unsure of the reason it is sitting there for a while but nonetheless, I wouldn't use what you are using.

//table[@width=200]/tbody/tr[1]/td/a/text() is not going to give you actual WebElement's therefore, I'd take a different approach:

//table[@width=200]/tbody/tr[1]/td/a and then call .getText() on all the elements it finds - the .getText() method is there for this very reason.

Arran
  • 24,648
  • 6
  • 68
  • 78
  • Thanks a lot for help and attention. Really the text() function dont work, with your help i found another problems. {***** the xpath was enclosed on apostrophes not only on quotation marks} {*****its dont work, i get on neverending wait: "//table[@width=200]/tbody/tr[1]/td/a/text()"} {*****searching about text() function on selenium i found it but using it i need to call gettext on same way, there is no advantage: "//table[@width=200]/tbody/tr[1]/td/a[text()]"} {*****using your suggestion and it working fast now: "//table[@width=200]/tbody/tr[1]/td/a"} – newway May 19 '13 at 23:58