【问题标题】:Unable to locate element 'Add to Cart ' on Ted Baker ecommerce site无法在 Ted Baker 电子商务网站上找到元素“添加到购物车”
【发布时间】:2017-07-05 18:04:36
【问题描述】:

enter image description here我无法使用 Xpath 和类名找到 Web 元素添加到购物车。 步骤:尝试在购物车/愿望清单中添加产品,但按钮不可点击。 谁能帮我解决这个问题。
==================================================== ========================== 代码>>>

    package Test;
    import java.util.HashMap;
    import java.util.Map;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    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;

    public class tedbak {

        public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub
            WebDriver driver;

                System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe");
                //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
             Thread.sleep(2000);
            ChromeOptions options = new ChromeOptions();
            Map<String, Object> prefs = new HashMap<String, Object>();
            prefs.put("credentials_enable_service", false);
            prefs.put("password_manager_enabled", false); 
              options.setExperimentalOption("prefs", prefs);
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            driver = new ChromeDriver(options);

                driver.get("http://www.tedbaker.com/");


                WebDriverWait wait = new WebDriverWait(driver, 40);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")));
                        if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed())
                            {

                            driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click();

                            WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]"));
                            Search_Item.click();
                            WebElement Seacrh_item_message=driver.findElement(By.id("search"));
                            WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input"));

                            Search_textbox.sendKeys("Watches");
                            driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click();
                            WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1"));
                            driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click();

 WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a"));
                            JavascriptExecutor jse1= (JavascriptExecutor)driver;
                            jse1.executeScript("window.scrollBy(0,400)", "");
                            Item_Tobuy.click();
                                                         driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click();
                           Select dropdown1=new Select(driver.findElement(By.id("qty")));
                           dropdown1.selectByVisibleText("5");
                           driver.findElement(By.id("qty")).click();
                           driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]")).click();
                                                                            }

    }
    }
===========================================================================    
    error>>>

    Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 17869
    Only local connections are allowed.
    Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]"}
      (Session info: chrome=59.0.3071.86)
      (Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 52 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
    System info: host: 'D90ZC6Q1', ip: '192.168.163.235', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_92'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\DAHIBH~1\AppData\Local\Temp\scoped_dir8284_18567}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.86, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
    Session ID: b48bc601ec61113cfada0c7c2f5f77b8
    *** Element info: {Using=xpath, value=html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/div[3]/div[2]/a/span[1]/span[1]}
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
        at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
        at Test.tedbak.main(tedbak.java:70)

【问题讨论】:

    标签: selenium-webdriver


    【解决方案1】:

    您需要将代码中的最后一个 XPath 更改为 "//*[@id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]"

    driver.findElement(By.id("qty")).click();
    driver.findElement(By.xpath("//*[@id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click();
    

    【讨论】:

    • 谢谢,自定义路径正在定位添加到购物车的元素
    【解决方案2】:

    尝试使用 xpath //input[contains(@class,'add_to_cart')]

    【讨论】:

    • 谢谢,自定义路径正在定位添加到购物车的元素
    • 请检查 html 代码中唯一的属性或属性名称,并基于它构造 xpath。例如在这里,我发现 add_to_cart 在 html dom 中是唯一的并使用它。请有一些关于xpath构造的知识。
    【解决方案3】:

    首先,请尽量不要使用绝对 xpath 定位器。这是一个非常糟糕的定位元素策略。尝试至少有相对定位器或根本没有 xpath。请参阅此post 和此post,了解如何使用好的 Selenium 选择器。

    其次,您可以根据您的情况使用以下选择器

    driver.findElement(By.cssSelector("div.product_add.ng-scope")).click()
    

    成功了。

    甚至这个,

    driver.findElement(By.cssSelector("div.button_success.major")).click()
    

    做同样的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 2015-11-15
      • 1970-01-01
      相关资源
      最近更新 更多