Reputation: 11
I have a Selenium Webdriver JUnit Java test and I am unable to use locators successfully within a Drupal 8 table (which also has styling). In Firefox I can use Firepath and Xpath checker to determine and check the xpath OK. However when I run the JUnit 4 test it is unable to be located. What was interesting is that the first time I ran this test it worked ok, however many attempts afterwards and it will not run.
I have used xpath etc successfully for many other tests within this web site, but for some reason I cannot get this to work.
Actually the unable to locate element message is for the next command;
driver.findElement(By.id("edit-application-status-1")).click();
however I know it is failing on the previous command (as below) because it never clicks to get to this page.
Failing code;
driver.findElement(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td[2]/a")).click();
also tried;
driver.findElement(By.cssSelector("a[href='/application-19']")).click();
I have tried changing to cssSelector
with explict wait as below
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href='/application-19']")));
scroll into view as below;
WebElement element1 = driver.findElement(By.cssSelector("a[href='/application-19']"));
jse.executeScript("arguments[0].scrollIntoView(true);", element1);
Upvotes: 1
Views: 1323
Reputation: 3384
Handling Web table could be tricky so it's better to traverse all the elements of WebTable one by one ; then check if your element has been found and then trigger your action:
List<WebElement> tableElements = driver.findElements(By.xpath(".//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]/td"));
for(WebElement item : tableElements){
System.out.println("item text : " + item.getText());
if(item.getText().equals("Text of your <a> tag")){
WebElement newItem = item.findElement(By.tagName("a"));
newItem.click();
}
}
Upvotes: 0
Reputation: 11
//Click on first item in list tr[1]
System.out.println("Before");
List<WebElement> tableElements = driver.findElements(By.xpath(".//[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]"));
for(WebElement item : tableElements){
if(item.getText().equals("Abhimanyujeet")){
WebElement newItem = driver.findElement(By.xpath("/a"));
System.out.println("tableElements" + tableElements);
System.out.println("item" + item);
newItem.click();
}
}
System.out.println("After");
driver.findElement(By.id("edit-application-status-1")).click();
Console;
Before
tableElements[EyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9- e491-4afd-9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content'] /div/div/div[2]/table/tbody/tr[1]]]
itemEyesRemoteWebElement:[[FirefoxDriver: firefox on XP (acecd5e9-e491-4afd- 9f78-82351e50b9e4)] -> xpath: .//*[@id='block-ua-theme-content']/div/div/div[2]/table/tbody/tr[1]]
After
Trace;
org.openqa.selenium.NoSuchElementException: Unable to locate element: #edit\-application\-status\-1
Upvotes: 0