我在flipkart.com上练习。我无法在搜索结果后点击该产品。 xpath是正确的。我尝试使用滚动功能,可见,通过部分链接文本查找,等待,睡眠仍然无法做到。请找到以下代码。
package Flipkart;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TC2
{
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath(".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
}
}
Error:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 17575
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]"}
(Session info: chrome=58.0.3029.81)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 76 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'D90T0CQ1', ip: '192.168.162.83', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_102'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\SALUNK~1\AppData\Local\Temp\scoped_dir12552_16506}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.81, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a8c3432c9b1e8cce3365db9407fc310f
*** Element info: {Using=xpath, value=.//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
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 Flipkart.TC2.main(TC2.java:15)
答案 0 :(得分:0)
我尝试过你提到的相同代码行,但事情对我有用。也许你应该再试一次。
请尝试使用小 xpath :
<强>建议:强>
替换Xpath: html / body / div [1] / div / header / div [1] / div [2] / div / div / div [2] / form / div / div [2] /具有不同Xpath的按钮: 的 //形式[@Action =&#34; /搜索&#34;] / DIV / DIV [2] /按钮强>
您应该使用相对路径而不是绝对路径,因为如果元素路径中有任何更改,那么XPath就会失败。
答案 1 :(得分:0)
最终解决方案,我不能提出更多建议,
//openChromeBrowser();
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("//form[@action='/search']/div/div[2]/button")).click();
Thread.sleep(5000);
driver.findElement(By.linkText("Puma PUMA Zipper Backpack 26 L Laptop Backpack")).click();
1&GT;给一些等待时间,它的工作原理 2 - ;对于显示的任何项目,请使用LinkText作为定位器。
并遵循@Monika Singhal所说的自动化快速高效的结果。
答案 2 :(得分:0)
我得到了相同的异常并隐含地添加等待。现在工作正常。代码
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath("//*[@class='_2cLu-l']")).click();`
答案 3 :(得分:0)
以下是您的问题的答案:
我的代码中没有发现任何此类错误/问题。关于解决方案的几句话:
maximized
,因此您需要使用maximize
课程ChromeOptions
Chrome浏览器。NoSuchElementException
ChromeOptions
类禁用信息栏。xpath
看起来很容易受到攻击,请尝试为xpath
上的元素构建逻辑HTML DOM
。https://www.flipkart.com/
,搜索Bag
,点击Skybags Pixel Plus 03 30 L Backpack
并在新标签页中打开Skybags Pixel Plus 03 30 L Backpack (Grey)
。以下是修改过的代码块:
System.setProperty("webdriver.chrome.driver","C:\\your_directory\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.flipkart.com/");
driver.findElement(By.name("q")).sendKeys("Bag");
driver.findElement(By.xpath("html/body/div[1]/div/header/div[1]/div[2]/div/div/div[2]/form/div/div[2]/button")).click();
driver.findElement(By.xpath(".//*[@id='container']/div/div[2]/div[2]/div/div[2]/div/div[3]/div[1]/div[1]/div[1]/div/a[2]")).click();
如果这回答你的问题,请告诉我。