【问题标题】:Unable To Click Paypal Button Continue With Selenium无法单击 Paypal 按钮继续使用 Selenium
【发布时间】:2018-09-01 06:45:15
【问题描述】:

我的代码首先让我登录 PayPal,然后登录 eBay 并导航到支付费用页面,然后使用 PayPal 结账。我无法单击/提交的最后一个“继续”按钮。我已经尝试过 xpath、id 和 class。我什至尝试发送 TAB 7x 直到 Continue 按钮,然后发送 Enter 但这不起作用。

我找到了这个讨论,但我不确定如何让它对我有用。 PayPal Sandbox checkout 'continue button' - Unable to locate element: - C# WebDriver

这是我正在尝试做的 PayPal 代码和页面的屏幕截图。

    //Chrome WebDriver specific
    System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize(); //maximise webpage
    WebDriverWait wait = new WebDriverWait(driver, 20);

    //navigate to Paypal
    driver.get("https://www.paypal.com/uk/signin");

    //wait 2.5s for the page to load
    try {
        Thread.sleep(2500);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    WebElement paypalEmail = driver.findElement(By.id("email"));
    paypalEmail.sendKeys("******");

    //wait 2.5s for the page to load
    try {
        Thread.sleep(2500);
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    WebElement paypalSubmit = driver.findElement(By.id("btnNext"));
    paypalSubmit.click();

    String URL = ("https://www.paypal.com/uk/signin");
    driver.get(URL);
    WebElement form2 = driver.findElement(By.cssSelector(".main form"));
    WebElement username = form2.findElement(By.id("password"));
    username.sendKeys("******");

    WebElement paypalSubmit2 = driver.findElement(By.id("btnLogin"));
    paypalSubmit2.click();

    //navigate to Ebay
    driver.get("https://signin.ebay.co.uk/ws/eBayISAPI.dll?SignIn&ru=https%3A%2F%2Fwww.ebay.com%2F");

    // Enter user name , password and click on Signin button
    WebElement form = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#mainCnt #SignInForm")));

    form.findElement(By.cssSelector("input[type=text][placeholder='Email or username']")).sendKeys("******");
    form.findElement(By.cssSelector("input[type=password]")).sendKeys("******");

    form.findElement(By.id("sgnBt")).click();

    driver.get("http://cgi3.ebay.co.uk/ws/eBayISAPI.dll?OneTimePayPalPayment");

    //WebElement Pay =
    driver.findElement(By.xpath("//input[@value='Pay']")).click();

    WebDriverWait wait2 = new WebDriverWait(driver, 15);
    wait2.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"confirmButtonTop\"]")));
    driver.findElement(By.xpath("//*[contains(@id,'confirmButtonTop')]")).click();
}
}

【问题讨论】:

  • 没用,是什么都没有发生还是有什么问题,是不是抛出了异常?
  • @Frank 道歉,页面在最后结账时就坐在那里。代码运行良好,IntelliJ 上没有抛出任何错误
  • Id 'confirmButtonTop' 是唯一的,你检查了吗?
  • @Frank 我实际上不确定如何检查它是否是唯一的。我检查了页面源,找不到任何“confirmButtonTop”。我检查了元素,只有那个。
  • 这里解释:stackoverflow.com/q/22571267/6320173最好使用Chrome开发者工具。

标签: java selenium button


【解决方案1】:

根据您给定的屏幕截图,以下之一应该可以单击继续按钮:

方法一:

  WebElement paypalSubmit = driver.findElement(By.xpath("//input[@data-test-id='continueButton']"));
  paypalSubmit.click();

方法二:

By paypalButton=By.xpath("//input[@data-test-id='continueButton']"));
WebElement element=driver.findElement(paypalButton);    
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("arguments[0].scrollIntoView(true);",element);
js.executeScript("arguments[0].click();", element);

如果您觉得按钮需要滚动到底部才能点击,请尝试第二种方法。

如果上述方法不起作用,您可以为按钮使用另外一个 xpath:

//input[@value='Continue' and @id='confirmButtonTop']

【讨论】:

  • 感谢您的快速响应,非常感谢。不知何故,第一个和第三个选项不起作用。它进入Pay​​pal屏幕并坐在那里。我对向下滚动的第二个选项很感兴趣。但是,当我键入“js.execute”时,“js”会以红色显示“无法解析符号'js'”。如果这有什么不同,我正在使用 IntelliJ。
  • 我设法通过添加 JavascriptExecutor js = (JavascriptExecutor)driver; 来消除错误。但是,这仍然没有点击继续按钮。
  • 不,很遗憾,我还不能让它工作。
【解决方案2】:

根据我的经验,paypal 喜欢使用 iFrame。如果您的情况是这样,这意味着除非您告诉 webdriver 切换框架上下文,否则无论您的 xpath/css 选择器如何,您都无法使用该 paypal 表单。

您可以获得当前使用此代码加载的所有可用帧的列表:

String[] handles = driver.getWindowHandles()

您的实际页面将始终是该返回数组中的第 0 个索引。如果 paypal 是您唯一的 iFrame,那么您可以定位第一个索引。这是一个可能的解决方案:

String mainPageHandle = handles[0];
String paypalHandle = handles[1];

driver.switchTo().window(paypalHandle);
// Do paypal interactions

driver.switchTo().window(mainPageHandle);
// Back to the main page

肯定有更强大的方法来处理这个问题,如果不幸的是您的页面有多个 iFrame,那么您可能需要做更多的工作来验证哪个句柄是哪个句柄,例如测试您知道包含的元素是否存在之内。通常,帧每次都会以相同的顺序加载。作为解决此问题的黄金途径,这将让您进出 iFrame 以执行工作。

【讨论】:

    【解决方案3】:

    有时传统的click() 不起作用。在这种情况下,请尝试使用 Javascript 执行器 Click,如下所示。

    确保你导入了这个类

    org.openqa.selenium.JavascriptExecutor
    

    用这个代替click();

    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(“//input[@data-test-id='continueButton']”)));
    

    试试这个,让我知道这是否适合你。

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 2014-01-04
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 2019-05-30
      相关资源
      最近更新 更多