How to have selenium webdriver script click on "Continue with google" using any type of locator, current script returns "element not found"

huangapple go评论88阅读模式
英文:

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&lt;String, Object&gt; vars;
JavascriptExecutor js;
@BeforeEach
public void setUp() {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap&lt;String, Object&gt;();
}
@AfterAll
public static void tearDown() {
driver.quit();
}
public String waitForWindow(int timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
Set&lt;String&gt; whNow = driver.getWindowHandles();
Set&lt;String&gt; whThen = (Set&lt;String&gt;) vars.get(&quot;window_handles&quot;);
if (whNow.size() &gt; whThen.size()) {
whNow.removeAll(whThen);
}
return whNow.iterator().next();
}
@Test
public void test() throws InterruptedException {
driver.get(&quot;https://tinder.com/&quot;);
System.out.println(&quot;MOUNA CAMELIA&quot;);
driver.manage().window().setSize(new Dimension(1552, 840));
driver.findElement(By.xpath(&quot;//a//div[text()=&#39;Log in&#39;]&quot;)).click();
driver.findElement(By.xpath(&quot;//*[@id=\&quot;container\&quot;]/div/div[2]/span[1]&quot;)).click();
driver.switchTo().frame(0);
vars.put(&quot;window_handles&quot;, driver.getWindowHandles());
driver.findElement(By.cssSelector(&quot;.ssJRIf&quot;)).click();
vars.put(&quot;win8338&quot;, waitForWindow(2000));
vars.put(&quot;root&quot;, driver.getWindowHandle());
driver.switchTo().window(vars.get(&quot;win8338&quot;).toString());
driver.findElement(By.cssSelector(&quot;.fFW7wc-ibnC6b-r4m2rf:nth-child(4) &gt; .fFW7wc-ibnC6b-ssJRIf&quot;)).click();
driver.close();
driver.switchTo().window(vars.get(&quot;root&quot;).toString());
{
WebElement element = driver.findElement(By.linkText(&quot;Terms&quot;));
Actions builder = new Actions(driver);
builder.moveToElement(element).perform();
}
driver.findElement(By.cssSelector(&quot;.Mx\\(a\\):nth-child(4) path&quot;)).click();
driver.findElement(By.cssSelector(&quot;.Sq\\(28px\\) &gt; path&quot;)).click();}
}

I am using the XPATH &quot;//*[@id=\&quot;container\&quot;]/div/div[2]/span[1]&quot; but it's not working. I receive the error

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@id=&quot;container&quot;]/div/div[2]/span[1]&quot;}
(Session info: chrome=114.0.5735.110)

How to have selenium webdriver script click on "Continue with google" using any type of locator, current script returns "element not found"

I got the locator by right clicking on "Inspect element" and copying the XPATH from the inspector tools like shown below:
How to have selenium webdriver script click on "Continue with google" using any type of locator, current script returns "element not found"

答案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(&quot;//iframe&quot;));
//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();

huangapple
  • 本文由 发表于 2023年6月16日 09:49:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定