Advise on location elements on a webpage I am buildong a selenium script for

Answered

Hi there, I am after some advice regarding a Selenium script I am building. As can be seen from the screenshot attached I am hovering over the ‘Reports’ text from the navigation pane which brings up a menu…..from there I am selecting ‘Asset Management’, then ‘Terminated Report: Completed’ from the sub-menu.

I’ve also attached the code (‘Selenium Code’

Advise on location elements on a webpage I am buildong a selenium script for

) which highlights what I am trying to do….I thought the best way to do this would be via xpath, as there wasn’t a great deal of information to work off in terms of elements. I’ve attached the HTML from the webpage (Screenshot 2, which hopefully gives an idea of how I built my test scripts in Selnenium.

The issue I have had is that, as can be seen from Screenshot 1, I want to arrive at Asset Management > Terminated Report: Completed. After I have checked that page I want to go to Asset Management > Terminated  Report: Pending. However, the XPath when selecting the ‘Asset Management’ option is now different, even thought the selection of it is the same as before. I’m assuming this is because I am selecting it from a different page. Based on the above, I have two questions which I am hoping for assistance on:
1) Given that it looks like XPath isn’t the best way of me navigating through this navigation menus, what would be the best element locator to use, in order to get to the desired location? Hopefully, the screenshots provide enough detail.
2) Another issue I have is that when I

Advise on location elements on a webpage I am buildong a selenium script for

Advise on location elements on a webpage I am buildong a selenium script for

arrive at a page, using the above selenium code, the focus is still on the option I have selected from the menu e.g. ‘Terminated Report: Completed’. This prevents me from being able to select any elements on the page, and I can’t see any way around this. Is there an action whereby the focus can be switched to the webpage as a whole, so I can complete any actions I need to.

Apologies for the long-winded questions, but I just wanted to make sure I explained it all correctly. Any help would be greatly appreciated.

Andy

Train Asked on November 19, 2014 in Selenium WebDriver.
Add Comment
  • 5 Answer(s)
    Best answer

    HI Andrew,

    First I would like to appreciate your effort you put to write this question beautifully. I wish everybody follow these standards, it easy for us to investigate.

    Now coming back to you questions
    Question 1 : Avoid using Absolute xpaths, always prefer relative xpaths. Please read this article, you will find your answers by your own:
    http://www.toolsqa.com/selenium-webdriver/choosing-effective-xpath/

    Question 2: I think the only problem is with when you are clicking the ‘Terminate Report Complete‘. In your Page Object I can see that you are using action class for mouse hovering on this element to. But actually you do not need to mouse hover to click this element. So just change your Page Object of ‘TerminateReportCompleteFocus‘ function and directly use click() to perform click. I think by following this your second problem will also resolve.

    Suggestion : Make use of FirePath tool of Firefox. It will automatically give you the absolute xpath.
    http://www.toolsqa.com/selenium-webdriver/xpath-firebug-firepath/

    Do not forget to close the question, if it resolve your issue.

     Best Reagrds,

    Lakshay Sharma

    Professor Answered on November 20, 2014.

    Hi Laksh,

    many thanks for responding. I’ve managed to sort out the first issue, based on your comments.

    However, I am still having the same issue with the second question I raised. I did what you suggested and removed the ‘mouse hover’ actions and instead directly used a ‘click()’. The issue is that I must initially hover over the ‘Report’ text in order to display the menu and sub-menu. I don’t actually want to click on ‘Report’, though’ as doing so will take me to a page I don’t want to go to, so the hover action has to be done. What now happens is that when I arrive at the ‘Terminated Report: Completed’ page the focus remains on the text from the drop-down menu, and I cannot switch the focus to the page in order to complete any actions.

    I’ve attached a screenshot of the code snippet, which highlights the code changes I have made. Do you have any suggestions?

    on November 21, 2014.
    Add Comment

    Hi Laksh,

    many thanks for responding. I’ve managed to sort out the first issue, based on your comments.

    However, I am still having the same issue with the second question I raised. I did what you suggested and removed the ‘mouse hover’ actions and instead directly used a ‘click()’. The issue is that I must initially hover over the ‘Report’ text in order to display the menu and sub-menu. I don’t actually want to click on ‘Report’, though’ as doing so will take me to a page I don’t want to go to, so the hover action has to be done. What now happens is that when I arrive at the ‘Terminated Report: Completed’ page the focus remains on the text from the drop-down menu, and I cannot switch the focus to the page in order to complete any actions.

    I’ve attached a screenshot of the code snippet, which highlights the code changes I have made. Do you have any suggestions?

    RE: Advise on location elements on a webpage I am buildong a selenium script for


    Andy

    Train Answered on November 21, 2014.

    I meant to say this:

    Mouse Hover Report
    Mouse Hover AssetManagement
    Click on Terminate Report

    on November 21, 2014.

    Thanks Laksh, I’ve got around that but just have one final issue which I have detailed below. Hope you can help.

    on November 21, 2014.
    Add Comment

    Thanks for the response, Laksh. Now I’ve re-run the selenium test I reckon the issue is to do with the element I am trying to select from the ‘Terminated Report: ‘Completed’ page. I’ve attached a screenshot of the page below; as you can see, there is a bar graph on the page…..for my test I want to select one of the bars which will then open up other functionality on the page. However, I am still having the problem where the focus is on the text from the drop-down, BUT……when I select another element from the page, e.g. the ‘Update Report’ button I am able to do so successfully. This suggests that there may be an issue with selecting functionality from the bar graph. It’s a bit odd as when I open up this page manually, I click on the bar graph once and it carries out the desired action…..however, when I use Selenium and click on the bar graph it doesn’t allow me to select it. As mentioned earlier, the focus remains on the drop-down option so this may be causing the issue, but it didn’t cause any problems when clicking on ‘Update Report’, which is a bit odd.

    RE: Advise on location elements on a webpage I am buildong a selenium script for

    I’ve also attached the code I have written when I am trying to attempt to select one of the bars on the graph (see below)

    RE: Advise on location elements on a webpage I am buildong a selenium script for

    Hope you can help.

    Andy

    Train Answered on November 21, 2014.
    Add Comment

    Sorry, I attached the wrong screenshot of the code I wrote but cannot delete it. The correct code snippet is below

    RE: Advise on location elements on a webpage I am buildong a selenium script for

    Train Answered on November 21, 2014.

    Screen shot is too small for HTML to read. But it is a svg image element, may be this is the issue. Google it to handle svg img element with selenium, might get some clue.

    on November 21, 2014.

    Thanks Laksh, I have had a dig around on Google but not found anything that matches exactly what I am trying to do. I’ve posted something on the stack overflow forum which will hopefully help

    on November 24, 2014.

    I’m struggling to find anything to help me with this 🙁 Using my  example below is anyone able to offer me any guidance as to how I’d carry out a ‘click’ event on one of the bars?

    on November 24, 2014.
    Add Comment

    I’ve now managed to work this out, by writing the below code:

    public static void barChartSelector(InternetExplorerDriver driver)
    {
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement parent = driver.findElement(By.className(“highcharts-series-group”));
    List<WebElement> children = parent.findElements(By.tagName(“rect”));
    children.get(0).click();
    }

    Many thanks for your help

    Andy

    Train Answered on November 24, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.