Selenium element found in system but not found in

2019-05-12 00:02发布

问题:

My team and I have recently started working on developing automated scripts using Selenium Web Driver and JUnit. I am facing an issue, and I am literally out of ideas on how to proceed. Any suggestions would be useful.

Here is the problem: I have a page, in which I upload two excels in a form and then press the submit button to confirm the upload.

When the upload completes it populates the database with the data, and the next page confirms the number of rows for each excel.

I am trying to get the element that confirms the upload which is a 'label' type.

All of this works successfully on locally, on my pc (windows 7, firefox webdriver).

When uploading my code, and when jenkins builds/executes it I get a "Element not found" and the test fails.

Since I have no visual access to what jenkins runs, I substituted the element confirmation (which fails on jenkins) with getPageSource().

getPageSource() returns the page I expect to see with the element I am trying to find (both locally and on jenkins).

Any ideas as to what may be happening?

Notes:

There are two 10sec thread.sleep. One when uploading each excel, and one after clicking submit, to ensure that the next page loads and that the excels are submitted successfully.

I try to locate the element using xpath. In fact I used both the xpath given to me by Firebug in Firefox and web developer in Chrome. just to make sure there wasn't any problem with the xpath. Both xpaths locate the element successfully on my local machine but fail on jenkins.

Executing the test (locally and on Jenkins) I can see the database populate as it should.

Excuse the lack of code, but I am restricted as to what I can share.

回答1:

Try using fluentwait instead of Thread.sleep(). Your element might not have been loaded in 10 seconds you may try this

// Waiting 30 seconds for an element to be present on the page, checking
 // for its presence once every 5 seconds.
   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
   .withTimeout(30, SECONDS)
   .pollingEvery(5, SECONDS)
   .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
 public WebElement apply(WebDriver driver) {
   return driver.findElement(By.id("foo"));
 }
}); 

as given here.