I would like to have my selenium webdriver script to go and click on "Continuer avec Google", which is written in French for some reason. I am using an XPATH and the script fails. Here is my code:
package Test;
// Generated by Selenium IDE
import org.junit.Before;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class MounaTest {
private static WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@BeforeEach
public void setUp() {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@AfterAll
public static void tearDown() {
driver.quit();
}
public String waitForWindow(int timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
Set<String> whNow = driver.getWindowHandles();
Set<String> whThen = (Set<String>) vars.get("window_handles");
if (whNow.size() > whThen.size()) {
whNow.removeAll(whThen);
}
return whNow.iterator().next();
}
@Test
public void test() throws InterruptedException {
driver.get("https://tinder.com/");
System.out.println("MOUNA CAMELIA");
driver.manage().window().setSize(new Dimension(1552, 840));
driver.findElement(By.xpath("//a//div[text()='Log in']")).click();
driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[2]/span[1]")).click();
driver.switchTo().frame(0);
vars.put("window_handles", driver.getWindowHandles());
driver.findElement(By.cssSelector(".ssJRIf")).click();
vars.put("win8338", waitForWindow(2000));
vars.put("root", driver.getWindowHandle());
driver.switchTo().window(vars.get("win8338").toString());
driver.findElement(By.cssSelector(".fFW7wc-ibnC6b-r4m2rf:nth-child(4) > .fFW7wc-ibnC6b-ssJRIf")).click();
driver.close();
driver.switchTo().window(vars.get("root").toString());
{
WebElement element = driver.findElement(By.linkText("Terms"));
Actions builder = new Actions(driver);
builder.moveToElement(element).perform();
}
driver.findElement(By.cssSelector(".Mx\\(a\\):nth-child(4) path")).click();
driver.findElement(By.cssSelector(".Sq\\(28px\\) > path")).click();}
}
I am using the XPATH "//*[@id=\"container\"]/div/div[2]/span[1]"
but it's not working. I receive the error
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="container"]/div/div[2]/span[1]"}
(Session info: chrome=114.0.5735.110)
I got the locator by right clicking on "Inspect element" and copying the XPATH from the inspector tools like shown below:
If you notice the HTML, desired elements are wrapped within an iframe
, you need to switch into the frame and then perform other actions, use below code to switch to iframe
:
WebElement iframeElement = driver.findElement(By.xpath("//iframe"));
//now using the switch command
driver.switchTo().frame(iframeElement);
Once you have finished performing actions within iframe
, you can come back to main content by using below code:
driver.switchTo().defaultContent();