首页 > 解决方案 > 访问网站时无法发送密钥或关闭弹出窗口

问题描述

当我访问此站点时:https ://www.flipkart.com/

系统提示我输入用户名和密码,但我无法使用驱动程序将密钥发送到任一输入字段,我也无法使用 x 按钮上的单击方法跳过登录。我尝试使用 By.xpath和 By.className 与 WebElements 交互,但它不起作用。这是我的代码来显示我尝试过的内容。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

class Test{

public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver","C:\\SeleniumDrivers\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            driver.get("https://www.flipkart.com/");

            //I don't think the pop up is a frame. This doesn't work.
            driver.switchTo().frame(1);
            //className of the 'X' button. Doesn't work
            driver.findElement(By.className("_2AkmmA _29YdH8")).click();
            //two xpaths of the 'X' button. Doesn't work
            driver.findElement(By.xpath("//button[@innertext='✕']")).click();
            driver.findElement(By.xpath("/html/body/div[2]/div/div/button")).click();
            //trying to send text to the email field. Both className and xpath doesn't work
            driver.findElement(By.className("_2zrpKA _1dBPDZ")).sendKeys("test");
            driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("test1");


        }
    }

标签: javaseleniumautomation

解决方案


尝试使用相对 XPATH 来识别元素。在您的查询中,您可以选择以下任何 XPATH 是否有效。

//*[text() = 'Enter Email/Mobile number']//..//../input
//*[contains(text(),'Enter Email')]//..//../input
(//*[text() = 'Enter Email/Mobile number']//preceding::input)[last()]

您可以在Dev.to了解有关 XPATH 的更多信息


推荐阅读