1

I am using Selenium WebDriver with Java with ChromeDriver.

I want to click on a cell in the table shown below, but because the table overflows and has a scrollbar, there is no guarantee the cell is actually in the view.

enter image description here

I get the cell by its XPath based on some parameters relevant to my project. I tried something like the following

String cellXPath = "/html/body/div/div/section/div/div/div[2]/div/div/div/table/tbody/tr[2]/td[2]/div/table/tbody/tr[" + (c.getRow()+2) + "]/td/*[" + (c.getCol()+1) + "]";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(cellXPath)));
WebElement element = WebDriverUtility.driver.findElement(By.xpath(cellXPath));
System.out.println("CELL: " + element.toString());
jse.executeScript("arguments[0].scrollIntoView(true)", element);
Thread.sleep(500);
element.click();

The element prints out fine and has an xpath associated with it. No scrolling occurs and no clicking occurs.

Currently getting this error as well at the element.click() line:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

EDIT:

You can find the source for the page here: http://libcal.library.ucsb.edu/rooms.php?i=12405

I have tried the solution in the other question where I wait for the element to be attached, or a explicit Thread.sleep, but the issue is that the element is hidden in the table because when the page loads the table is automatically scrolled to the right a certain amount. Thus, the element can be found, but not clicked on. I need to put the element back into the view by invoking a scroll to the cell and then fire the click event. Hope that clarifies the comments

Special K
  • 55
  • 10
  • Please show the HTML and please make the shown HTML includes the element which holds the scrollbar (which define `overflow` in style). And confirm below question, the html code of those invisible cells is present the HTML code before you scroll them into view? – yong May 28 '18 at 03:21
  • Possible duplicate of [Random "Element is no longer attached to the DOM" StaleElementReferenceException](https://stackoverflow.com/questions/5709204/random-element-is-no-longer-attached-to-the-dom-staleelementreferenceexception) – Manmohan_singh May 28 '18 at 03:59
  • @SpecialK Why do you `element.toString()`? – undetected Selenium May 28 '18 at 04:39
  • At which line you are getting Error ? – Ishita Shah May 28 '18 at 05:03
  • Added edits to the question answered your concerns – Special K May 28 '18 at 09:23

2 Answers2

0

use ExpectedConditions.elementToBeClickable instead of ExpectedConditions.visibilityOfElementLocated

you can split it to:

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(cellXPath)));
pburgr
  • 1,722
  • 1
  • 11
  • 26
0

You can solve your scenario without using JavaScript Executor. Please find the below steps

Steps:

  1. Select the required row first
  2. Get the list of all the matched columns child tag from the matched the row
  3. Get the Matched Attribute Tag first and then perform the action based on the matched tag (Since, we are able to click only on the available slot and we are having different tag for the available/unavailable slot)

Working Code:

    //selectRow=Actual Row + 1. Since, first row is displayed for the heading
    int selectRow=11;
    int selectColumn=40;

    WebElement rowElement=driver.findElement(By.xpath("//table[@id='s-lc-rm-scrolltb']/tbody/tr["+selectRow+"]"));
    List<WebElement> availableSlotList=rowElement.findElements(By.xpath(".//td/*"));

    if(availableSlotList.get(selectColumn).getTagName().equals("a")){
        availableSlotList.get(selectColumn).click();
    }
    else{
        System.out.println("Booking Slot is Unavailable/Already Booked");
    }

Note: Please change the selectRow and selectColumn value using your logic. For Example, selectRow can be modified as c.getRow(). In addition to this, you can add one more condition for the ArrayIndexOutOfBoundsException case as well

Subburaj
  • 2,294
  • 3
  • 20
  • 37