英文:
How to have selenium webdriver script click on "Continue with google" using any type of locator, current script returns "element not found"
问题
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.chrome.ChromeDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
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();
// Here is the XPATH you mentioned:
driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[2]/span[1]")).click();
// Rest of your test steps...
}
}
The XPATH you provided seems correct. However, it's possible that there might be a timing issue or the element may not be present at the moment the script tries to click it. You can try adding explicit waits to ensure that the element is available before clicking it.
英文:
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:
答案1
得分: 1
如果你注意到了HTML,所需的元素被包裹在一个 iframe
中,你需要切换到这个框架然后执行其他操作,可以使用以下代码切换到 iframe
:
WebElement iframeElement = driver.findElement(By.xpath("//iframe"));
//现在使用切换命令
driver.switchTo().frame(iframeElement);
一旦你在 iframe
中完成了操作,可以使用以下代码返回到主内容:
driver.switchTo().defaultContent();
英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论