首页 > 解决方案 > 通过 selenium 在 paytm 主页上发起搜索的问题

问题描述

以下是我想通过 Selenium 在 paytm 上自动化的步骤。

脚步:-

1.启动支付宝。

2.在paytm页面顶部显示的搜索框中输入任何关键字。例如“移动”

3.按回车键导航到搜索结果页面。

问题:在搜索框中写入的关键字被自动删除

我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class XPath {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\ProgramFiles\\Work\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("https://paytm.com");
        //driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys("mobile");
        driver.findElement(By.xpath("//input[@placeholder='Search for a Product , Brand or Category']")).sendKeys(Keys.ENTER);
    }
}

标签: javaseleniumselenium-webdriverpaytmwebdriverwait

解决方案


问题是您需要等待页面完全加载。有一个在页面加载时运行的 ajax 脚本,它在搜索输入文本框上做了一些工作。当页面完全加载时,body 标签包含一个 style 属性。通过使用 a和a来等待此属性。ExpectedConditionsattributeCOntainsWebDriverWait

试试这个代码 -

new WebDriverWait(driver, 3).until(ExpectedConditions.attributeContains(By.tagName("body"), "style", "overflow: visible;"));
WebElement srch = driver.findElement(By.cssSelector("input[type='search']"));
srch.sendKeys("Hello");

推荐阅读