简体   繁体   中英

Selenium - How do click on this span class

I am not able to use xpath for this span class. I need to click on the link.

<span class="nav-selection">Ad Style</span>

I have been trying using this method:

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

The html code for the portion that I need to test.

<ul class="nav nav-side-menu" style="top: 0px;">
        <li class="shadow-layer"></li>
        <li>
            <a href="portal.do?portalCategory=analyst">
                <i class="icon-photon dashboard"></i>
                <span class="nav-selection">Dashboard</span>
                                </a>
        </li>

        <li>
            <a href="javascript:;" class="sub-nav-container">
                <i class="icon-photon adcorner"></i>
                <span class="nav-selection">Ad Corner</span>
                <i class="icon-menu arrow"></i>                </a>
            <div class="sub-nav adCorner">
                <ul class="nav">

                    <li class="menu-add">
                        <a href="showstatusreports.do">Status Reports</a>
                    </li>


                    <li class="menu-add">
                        <a href="listinsertion.do">Insertion Order</a>                              
                    </li>
                    <li class="menu-add">
                        <a href="campaignWrapper.do?processpath=defaultPage&amp;camStatus=L">Campaigns</a>

                        <i class="icon-add" title="create new campaign" rel="createCampaign"></i>   

                    </li>






                    <li class="menu-add">
                        <a href="campaigncreatepage.do">Tags</a>                            
                    </li>







                        <li class="menu-add">
                        <a href="viewcreative.do">View Creative</a>
                        </li>


                </ul>
            </div>
        </li>

Thanks for the help! Please let me know.

This is what i am writing in eclipse. please go through it and let me know. Thanks.

//class to run the test
public class AddNewCampaign extends TestLogin{

    WebDriver driver;

public static void main(String[] args) {

    AddNewCampaign addCamp = new AddNewCampaign();
    addCamp.LaunchParisUI();
    addCamp.loginCredentials();
    addCamp.addCampaign();


}

//method to add campagin

    public void addCampaign() { 

            WebElement heightclass = driver.findElement(By.xpath("//*[.='Ad Corner']/.."));

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

            WebDriverWait wait = new WebDriverWait(driver, 10);

            Actions builder = new Actions(driver);
            builder.moveToElement(heightclass).perform();
            wait.until(ExpectedConditions.visibilityOf(linkCampaigns));
            builder.moveToElement(linkCampaigns).click().perform();

        }

//class the launch the url

    public class LaunchParis{

        WebDriver driver;

        //Launch PairsUI    
        public void LaunchParisUI() {
            driver = new FirefoxDriver();
            //driver = new ChromeDriver();
            driver.get("https://xfpqa1uiadm1");
            driver.manage().window().maximize();
            }

From the very limited information you posted, you probably want:

driver.findElement(By.xpath("//span[@class='nav-selection']/parent::a"));

Find the span element with class nav-selection , and go to that element's parent anchor. Span is very seldom clickable, anchors is what you want to click on.

The following can be another option. Before click you want to make sure you are letting element load. This is text based search. And, a good way to bypass the element having same classname or attribute value

WebElement heightClass = driver.findElement(By.xpath("//*[.='Ad Corner']/.."));

The problem is you have two instances of driver . You have one instance in AddNewCampaign . You have another instance of driver in LaunchParis .

So you're fine in LaunchParis , because you've created a local driver , which you then use to load the web page with the driver.get() . After that, you return to the main , which still has an un-instantiated driver .

I'm not sure how exactly your code is structured. You may be able to clear this problem simply by removing the WebDriver driver definition from LaunchParis , or you may need to pass driver to LaunchParis .

So, possibly something like this:

public class LaunchParis{

    WebDriver localDriver;

    //Launch PairsUI    
    public void LaunchParisUI(WebDriver driver) {
        localDriver = driver;
        localDriver = new FirefoxDriver();
        //localDriver = new ChromeDriver();
        localDriver.get("https://xfpqa1uiadm1");
        localDriver.manage().window().maximize();
    }

And you would call it like this:

addCamp.LaunchParisUI(driver);

I'm not entirely sure how that would work in Java, ie if assigning localDriver = driver would set the AddNewCampaign instance of driver as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM