无法在 Ted Baker 电子商务网站上找到元素'Add to Cart '



在此处输入图像描述我无法使用 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:UsersDAHIBH~1AppDataLocalTempscoped_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)

您需要将代码中的最后一个 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();

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

首先,请尽量不要使用绝对的 xpath 定位器。这是一个非常糟糕的元素定位策略。尝试至少具有相对定位器或根本没有 xpath。请参阅这篇文章,以及这篇关于使用好的硒选择器的文章。

其次,对于给定的情况,您可以使用以下选择器

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

成功做到了。

甚至这个,

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

做同样的工作。

相关内容