Selenium xpath在运行时更改

时间:2016-08-19 17:48:05

标签: selenium selenium-webdriver

场景 - 有效登录www.homeshop18.com,然后从数字菜单中选择"三星"。 结果显示,现在我需要选择另一个品牌 - 品牌部分的Micromax(显示在页面左侧) 这需要滚动并选择Micromax。

问题:

虽然Micromax的xpath是正确的// * [@ id =' filter_1Option_12'] // div [@class =' ez-checkbox']但我看到了脚本的运行时间 - 其他一些品牌正在被选中而不是micromax。 请建议。

//有效登录www.homeshop18.com的课程

public class HomeShop_Login_Test 
{
    @FindBy(xpath="//a[@id='signInHeaderLink']") WebElement SignIn_Link;
    @FindBy(xpath=".//input[@id='emailId']") WebElement Email;
    @FindBy(xpath=".//input[@id='existing_user_radio']") WebElement Existing_User_Radio;
    @FindBy(xpath=".//input[@id='new_user_radio']") WebElement New_User_Radio;
    @FindBy(xpath=".//input[@id='password']") WebElement Password;
    @FindBy(xpath=".//a[@id='signin']") WebElement SignIn_Button;
    @FindBy(xpath="//a[@title='Close']") WebElement Close_Home;


    public void Login_Valid()
    {
        WebDriverWait wait = new WebDriverWait(driver, 30);

        WebElement SignIn_Link = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@id='signInHeaderLink']")));

        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("arguments[0].click()", SignIn_Link);

        Email.sendKeys("xxx@gmail.com");
        boolean selected;

        selected = New_User_Radio.isSelected();

        if(selected)
        {
            Existing_User_Radio.click();
        }

        Password.sendKeys("xxx");
        SignIn_Button.click();


    }

//Class to choose Samsung from Digital menu

public class Browse_Samsung_Mobile 
{

    @FindBy(xpath="//*[@id='CategoryMenu1']//a[text()='Digital']") WebElement Digital_Menu;
    @FindBy(xpath="//*[@id='CategoryMenu1']//a[@title='Samsung']") WebElement Samsung_SubMenu;
    @FindBy(xpath="//*[@id='filter_1Option_19']//span[@class='selected_filter_img']") WebElement Micromax;

    public void Browse_Samsung() 
    {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        Actions act = new Actions(driver);
        act.moveToElement(Digital_Menu).perform();
        act.click(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='CategoryMenu1']//a[@title='Samsung']")))).build().perform();

        //WebElement Micromax = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='filter_1Option_12']//span[@class='selected_filter_img']")));

        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("arguments[0].click()", Micromax);

    }
}

//class to call above two classes

public class Validate_Browse_Samsung_Mobile 
{

    WebDriver driver;

    @Test
    public void Validate_Browse() 
    {
        driver = BrowserFactory.getBrowser("Firefox");
        driver.get(DataProviderFactory.getConfig().getURL());
        HomeShop_Login_Test login = PageFactory.initElements(driver, HomeShop_Login_Test.class);
        login.Login_Valid();
        Browse_Samsung_Mobile browse = PageFactory.initElements(driver, Browse_Samsung_Mobile.class);
        browse.Browse_Samsung();

    }

}

2 个答案:

答案 0 :(得分:1)

您应该使用title属性尝试使用他们的名字,如下所示: -

WebElement micromax = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a[title ~= "Micromax"] input")));

答案 1 :(得分:0)

您应该使用以下XPath选择正确的复选框:

//a[@title="GSM Mobile Phones - Micromax"]/div/input