Selenium newbie here. The examples I tried so far worked well but now I stumbled upon a case that seemingly doesn't work:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GeckoDriverTest
{
public static void main(String[] args) throws Throwable
{
System.setProperty("webdriver.gecko.driver", "D://XXX/seleniumdrivers/geckodriver.exe");
FirefoxDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.aerzen.com/de/karriere/stellenangebote.html");
System.out.println("URL = " driver.getCurrentUrl());
Thread.sleep(3000);
driver.findElement(By.cssSelector("a.consent-banner--accept.button.submit")).click();
Thread.sleep(3000);
System.out.println("URL = " driver.getCurrentUrl());
List<WebElement> elements = driver.findElements(By.cssSelector("tr > td > a"));
System.out.println("Elements: " elements.size());
for(WebElement element : elements)
{
String url = element.getAttribute("href");
System.out.println(url);
}
}
}
The first part does work but the "tr > td > a" selector doesn't find any elements although I'm pretty sure they exist (I can see them in the browser window).
Any idea what's going wrong there? Thanks a lot.
CodePudding user response:
The table is inside an iframe. In order to access these elements you will need to switch to that iframe.
Also instead of tr > td > a
cssSelector try using table td.real_table_col1 a
cssSelector.
This should work better:
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//iframe")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe")));
List<WebElement> elements = driver.findElements(By.cssSelector("table td.real_table_col1 a"));
System.out.println("Elements: " elements.size());
for(WebElement element : elements)
{
String url = element.getAttribute("href");
System.out.println(url);
}
Also instead of hardcoded pauses like
Thread.sleep(3000);
You should use ExpectedConditions
something like
WebdriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.consent-banner--accept.button.submit")));