0

I have two elements on my page (two 'cancel' elements).

<div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">
Cancel
</div>

<div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">
Cancel
</div>

How do I click on the second element? Obviously, I can't us id because it is randomly generated on each visit. What can I use?

2 Answers 2

2


1. Use FindElements method, which finds all IWebElements within the current context using the given mechanism. (In this case, you always need to know the index of the element you are looking for.)

IWebDriver driver = new FirefoxDriver();
IList<IWebElement> cancelDivs = driver.FindElements(By.XPath("//div[text()='Cancel']"));
cancelDivs[1].click(); //zero-base index


2. If those cancel buttons are in different sections, which can be identified by non-ExtJS id attributes.

<div id='header'>
    <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">Cancel</div>
</div>
<div id='footer'>
    <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">Cancel</div>
</div>


IWebElement secondCancelDiv = driver.FindElement(By.XPath("//div[@id='footer']//div[text()='Cancel']"));
secondCancelDiv.Click();


3. If those cancel buttons are in different sections, which can be identified by different ExtJS class attributes. (use the meaningful ones)

<div id='ext-gen1060' class='x-grid3-body'>
    <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen1179">Cancel</div>
</div>
<div id='ext-gen2555' class='x-toolbar-right-row'>
    <div unselectable="on" class="x-grid-cell-inner x-unselectable" style="text-align: left; " id="ext-gen2951">Cancel</div>
</div>


IWebElement secondCancelDiv = driver.FindElement(By.XPath("//div[@class='x-toolbar-right-row']//div[text()='Cancel']"));
secondCancelDiv.Click();
Sign up to request clarification or add additional context in comments.

Comments

0

If:

  1. there are always only 2 'Cancel' buttons on the page, and
  2. you always need the 2nd one,

use //div[text()="Cancel"][2] xpath selector, or just find both of them and click the 2nd one.

1 Comment

More correctly, xpath=(//div[text()="Cancel"])[2]. That will work even if the two div elements aren't siblings.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.