java - 访问网站时无法发送密钥或关闭弹出窗口
问题描述
当我访问此站点时: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");
}
}
解决方案
尝试使用相对 XPATH 来识别元素。在您的查询中,您可以选择以下任何 XPATH 是否有效。
//*[text() = 'Enter Email/Mobile number']//..//../input
//*[contains(text(),'Enter Email')]//..//../input
(//*[text() = 'Enter Email/Mobile number']//preceding::input)[last()]
您可以在Dev.to了解有关 XPATH 的更多信息
推荐阅读
- python - 使用python返回一个非定向路径图
- perl - 尽管“没有警告 qw(experimental::signatures)”,但生成警告消息的签名
- flutter - 我可以从 Flutter 的对话框中更新父状态吗?
- selenium - 使用 selenium 和 javascript 在 chorme 浏览器上自动进行 gmail 身份验证
- java - 带有 Spring Boot CacheManager 的咖啡因异步缓存
- php - 无法让 phpmyadmin 列出多个服务器
- swiftui - 如何根据来自 ObservableObject 的 @Published 数组可靠地更新 SwiftUI 中的字符串?
- python - Django Hosted on Apache: Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding Python runtime state:
- node.js - Express & MongoDB:在时间戳创建的重复文档
- blockchain - 我正在使用 Polkadot js api 进行交易,但我无法获取交易完成或失败的事件