2016-08-16 237 views
0

我正在練習硒鼠標懸停和使用網站 - http://www.flipkart.com,我從「電子」菜單中選擇「三星」。下面是我的代碼,但即使xpaths正確,它也不起作用。Selenium:無法找到元素

@CacheLookup 
@FindBy(xpath="//a[@data-tracking-id='electronics']") WebElement Electronics_Menu; 
@CacheLookup 
@FindBy(xpath=".//*[@id='menu-electronics-tab-0-content']//a[text()='Samsung']") WebElement Samsung_Mobile_Click; 

Actions act = new Actions(driver); 
act.moveToElement(Electronics_Menu).perform(); 
act.click(Samsung_Mobile_Click).build().perform(); 

這是我的錯誤,當我運行代碼:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='menu-electronics-tab-0-content']//a[text()='Samsung']"} 
Command duration or timeout: 27.04 seconds 
+0

試試這個'XPath':'// a [@ title =「Samsung」]' – Andersson

+0

Thanks !!它的工作:) – Bimlesh

回答

0

試試三星的XPath在下面的代碼中給出的一個,它爲我工作。

public class FlipK { 

    WebDriver driver; 

    @CacheLookup 
    @FindBy(xpath="//a[@data-tracking-id='electronics']") 
    WebElement Electronics_Menu; 

    @CacheLookup 
    @FindBy(xpath=".//*[@id='container']/div/header/div[2]/div/ul/li[1]/ul/li/ul/li[1]/ul/li[2]/a/span[1]") 
    WebElement Samsung_Mobile_Click; 

    @Test 
    public void f() throws InterruptedException { 

    driver = new FirefoxDriver(); 
    driver.get("https://www.flipkart.com/#"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

    PageFactory.initElements(driver, this); 

    Actions act = new Actions(driver); 
    act.moveToElement(Electronics_Menu).perform(); 
    Thread.sleep(2000); 
    Samsung_Mobile_Click.click(); 
    } 
} 
+0

嗨prashanth,並歡迎堆棧溢出。我已經編輯了您的問題,使用markdown標記標記出您的代碼;這很重要,否則無法讀取代碼。 –

+0

謝謝文斯。我很感激! – prashanth