页面在获取当前位置时未加载

Posted

技术标签:

【中文标题】页面在获取当前位置时未加载【英文标题】:Page doesn't load as it fetches the current location 【发布时间】:2020-04-21 19:48:26 【问题描述】:

我正在尝试自动化以下场景:

    访问 BestBuy.com 搜索笔记本电脑 从结果页面点击特定的 HP 笔记本电脑 点击加入购物车 点击查看购物车 添加邮政编码并点击更新按钮并点击继续结帐按钮 现在点击继续链接 输入所有详细信息并单击继续 输入错误的信用卡详细信息并点击继续 验证错误消息

在第 7 步之后,它会自动检测我的当前位置并将其添加到 url(例如,https://www.bestbuy.ca/checkout/?qit=1#/en-ca/shipping/GJ/390020)and 页面未加载,这导致我的代码失败。我阻止网站获取当前位置(通过弹出窗口)但它没有帮助。 谁能建议我如何解决这个问题。

请查看以下代码:

package seleniumtestingscript;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.javascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class BestBuyScenario 

    WebDriver driver;
    WebDriverWait wait;
    JavascriptExecutor js;
    Select select;

    @BeforeMethod
    public void launch_Browser() 
        System.setProperty("webdriver.chrome.driver", "G:\\Sheetal\\Selenium_Program_Practice\\Driver\\chromedriver.exe");
        driver = new ChromeDriver();
        String URL = "https://www.bestbuy.com/";
        driver.get(URL);
        driver.manage().window().maximize();

    

    @Test(priority=1)
    public void HappyPath() throws InterruptedException 

    //Click on Canada link
    WebElement Canada_Link = driver.findElement(By.linkText("Canada"));
    Canada_Link.click();

    //wait till search page loads
    wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("search")));

    WebElement search_field = driver.findElement(By.xpath("//div/input[@aria-label = 'Search Best Buy']"));
    search_field.clear();
    search_field.sendKeys("Laptops");

    //wait till auto complete list is displayed
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//ul/li/a[starts-with(@class,'autocompleteLink')]")));

    //Click on laptops displayed in autocomplete list
    List<WebElement> autocomplete_list = driver.findElements(By.xpath("//ul/li/a[starts-with(@class,'autocompleteLink')]"));
    for(int i = 0; i<autocomplete_list.size(); i++)
    
        if(autocomplete_list.get(i).getText().contains("laptops"))
        
            System.out.println(autocomplete_list.get(i).getText());
            autocomplete_list.get(i).click();
            break;

        
    
    System.out.println("After loop");
    Thread.sleep(5000);
    //wait till laptop page loads and verify the page title
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div/h1[contains(text(),'Laptops & MacBooks')]")));

    System.out.println("After title on laptop page");
    //Scroll page till HP laptop gets visible and click on that laptop link
    js = (JavascriptExecutor)driver;
    js.executeScript("window.scrollBy(0,1500);", "");
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'HP 15.6\" Laptop - Silver (Intel Core i3-1005G1')]"))).click();


    //click on Add to cart button
    Thread.sleep(5000);
    js.executeScript("window.scrollBy(0,150);", "");
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(text(),'Add to Cart')]"))).click();
    System.out.println("Add to cart clicked");
    //WebElement add_to_cart_button = driver.findElement(By.xpath("//span[contains(text(),'Add to Cart')]"));
    //add_to_cart_button.click();


    //Switch to pop up and click on view cart button

    driver.switchTo().activeElement();
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-automation='view-cart-confirmation']"))).click();
    //WebElement view_cart_button = driver.findElement(By.xpath("//button[@data-automation='view-cart-confirmation']"));
    //view_cart_button.click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("postalCode"))).clear();
    WebElement postal_input = driver.findElement(By.id("postalCode"));
    //postal_input.sendKeys(Keys.CONTROL, Keys.chord("a")); //select all text in textbox
    //postal_input.sendKeys(Keys.BACK_SPACE); //delete it
    postal_input.sendKeys("M9V1S3"); //enter new text
    driver.findElement(By.xpath("//button[@data-automation='enter-postal-code-button']")).click();

    //click on continue_to_checkout_button
    js.executeScript("window.scrollBy(0,350);", "");
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//div/div/a[@data-automation='continue-to-checkout'])[position()=2]"))).click();
    //WebElement continue_to_checkout_button = driver.findElement(By.xpath("(//div/div/a[@data-automation='continue-to-checkout'])[position()=2]"));
    //continue_to_checkout_button.click();

    //wait till continue button is displayed and then click on it
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-automation= 'guest-continue']/a")));
    WebElement continue_link = driver.findElement(By.xpath("//div[@data-automation= 'guest-continue']/a"));
    continue_link.click();



    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("email")));
    WebElement email_field = driver.findElement(By.id("email"));
    email_field.sendKeys("sheetal@test.com");
    WebElement firstName_field = driver.findElement(By.id("firstName"));
    firstName_field.sendKeys("Sheetal");
    js.executeScript("window.scrollBy(0,150);", "");
    WebElement lastName_field = driver.findElement(By.id("lastName"));
    lastName_field.sendKeys("Patel");
    WebElement addressLine_field = driver.findElement(By.id("addressLine"));
    addressLine_field.sendKeys("Etobicoke");
    WebElement city_field = driver.findElement(By.id("city"));
    city_field.sendKeys("Toronto");
    WebElement regionCode_field = driver.findElement(By.id("regionCode"));
    Select select = new Select(regionCode_field);
    select.selectByVisibleText("Ontario");
    WebElement postalCode_field = driver.findElement(By.id("postalCode"));
    postalCode_field.sendKeys("M5G 2C3");
    WebElement phone_field = driver.findElement(By.id("phone"));
    phone_field.sendKeys("8989564646");
    WebElement continue_button = driver.findElement(By.xpath("//span[contains(text(),'Continue')]"));
    continue_button.click();

    WebElement cardnumber_field = driver.findElement(By.id("shownCardNumber"));
    cardnumber_field.sendKeys(card_number);
    WebElement month = driver.findElement(By.id("expirationMonth"));

    select = new Select(month);
    select.selectByVisibleText("1");

    WebElement year = driver.findElement(By.id("expirationYear"));
    select = new Select(year);
    select.selectByVisibleText(card_year);

    WebElement cvv = driver.findElement(By.id("cvv"));
    cvv.sendKeys(card_cvv);

    WebElement continue_button1 = driver.findElement(By.xpath("//span[contains(text(),'Continue')]"));
    continue_button1.click();

    WebElement error_msg = driver.findElement(By.xpath("(//div[@class='error-msg'])[position()=1]"));
    String actual = error_msg.getText();
    String expected = "Invalid credit card number. Please check your credit card number or use another payment method.";
    Assert.assertEquals(actual, expected);


    






【问题讨论】:

天哪,你有你的卡号 cvv 和纯文本到期!!!立即取消该卡! 那些是错误的细节(随机数字)。添加用于自动化目的。 别担心。在金钱方面我总是很小心。 您可以尝试将 webdriver 的“意外警报行为”设置为接受。默认是在下一个动作之前通知哪个抛出错误。 @pcalkins 你能告诉我我该怎么做吗? 【参考方案1】:

我找到了解决方案。实际问题是每当我运行我的脚本时,即使我阻止获取该站点的当前位置,浏览器的新实例仍允许获取当前位置(单击 https://www.bestbuy.com/ url -> 站点设置-> 权限中显示的锁定图标 - > 阻止位置)。

我在 Google 上搜索 - 如何阻止地理位置弹出,发现很少有解决方案,以下解决方案有效:

    ChromeOptions options = new ChromeOptions();
   Map<String, Object> prefs = new HashMap<String, Object>();
                prefs.put("profile.managed_default_content_settings.geolocation", 2);
options.setExperimentalOption("prefs", prefs);
            driver = new ChromeDriver(options);

【讨论】:

以上是关于页面在获取当前位置时未加载的主要内容,如果未能解决你的问题,请参考以下文章

获取当前滚动位置并将其作为带有链接的变量传递?

页面重新加载时未命中更改功能

拖动地图时未获取当前位置

如果在页面初始加载时未选中复选框,则返回无效提交

字体文件在控制台上显示为未找到,并且在页面加载时未立即呈现

Jquery Geocomplete。在页面加载时显示用户当前位置