4

I have a button reach_analysis_button with id="x-auto-7".

But directly using that i am not able to locate the element from webdriver.

I tried ("//div(@class=’x-window-bwrap’)/div[@class=’x-window-ml’]/div[@class='x-window-mr']/div[@class=’x-window-mc’]/div[@role=’presentation’]/img[@id='x-auto-7']")) but was of no use.

Finally what helped me was the following:

webdriven.findElement(By.id("welcomeWindow"));
webdriven.findElement(By.className("x-window-bwrap"));
webdriven.findElement(By.className("x-window-ml"));
webdriven.findElement(By.className("x-window-mr"));
webdriven.findElement(By.className("x-window-mc"));
WebElement Estimates=webdriven.findElement(By.id("x-auto-7"));

Following is the html of image button reach_analysis_button I am trying to click:

<div class="x-shadow x-ignore" style="display: block; z-index: 1011; left: 416px; top: 9px; width: 580px; height: 292px;">
<div id="welcomeWindow" class=" x-window x-component " style="position: absolute; width: 580px; left: 406px; top: 0px; z-index: 1012;" tabindex="0" hidefocus="true">
<div class="x-window-tl" role="presentation">
<div class="x-window-bwrap" role="presentation" style="overflow: auto; position: static;">
<div class="x-window-ml" role="presentation">
<div class="x-window-mr" role="presentation">
<div class="x-window-mc" role="presentation">
<div class="x-window-body x-abs-layout-container" role="presentation" style="width: 566px; height: 261px;">
<img id="x-auto-7" class="welcome-image x-component x-abs-layout-item" src="resources/images/arbitron/reach_analysis_button.jpg" style="left: 193px; top: 6px;">
<img id="x-auto-8" class="welcome-image x-component x-abs-layout-item" src="resources/images/arbitron/saved_insights_button.jpg" style="left: 380px; top: 6px;">
<img id="x-auto-9" class="welcome-image x-component x-abs-layout-item" src="resources/images/arbitron/behavior_analysis_button.jpg" style="left: 6px; top: 6px;">

I need a single line locator for this element, other than using those six lines, is it possible?

6
  • 1
    so what do you want answered?
    – Ardesco
    Commented Aug 5, 2011 at 13:25
  • I should suffice to use an xpath of "//*[@id='x-auto-7']". Of course this the img element in question always has the same id. The name "x-auto-7" suggests the ID is auto-generated, and my experience is that auto-generated IDs tend to change.
    – user246
    Commented Aug 5, 2011 at 17:48
  • Ardesco I ran the test a few times but didn see the id changing , the problem is with directly locating to that xpath . If u see teh code i have given above, step by step geting to that location through the div hierarchy driver is able to locate it , but directly it cant Commented Aug 6, 2011 at 2:20
  • When I've had pages like this that are difficult to find I use Wait's to make sure the page is loaded, but looking for some text and then look for the page source. I avoid XPath since I mostly test on IE but if it works for you then do it.
    – MichaelF
    Commented Nov 7, 2011 at 12:57
  • yes Michael, I been using the same script since and its working quiet consistently . Commented Nov 8, 2011 at 10:57

2 Answers 2

3

Did you try using regular expression or a substring in the xpath?

Something like this may help:

 xpath = "//img[contains(@id,'x-auto')]" OR
WebElement Estimates=webdriven.findElement(By.xpath("//img[contains(@id,'x-auto')]"));

You can also use another xpath method starts-with(@id,'x-auto')

2

Have you tried using CSS selector to find the button as a descendent of the welcome window?

WebElement Estimates = webdriven.findElement(By.CssSelector("#welcomeWindow #x-auto-7"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.