1

I tried to automate a scenario, where the condition is that I have to select an option from drop down and then there's another dropown next to it, I have to click one option from next drop to enable to button . I tried with the code but it clicks only the first option,.And showing error as stale Element reference:element is not attached to the page document. Please help. Please let me know if in not very clear.

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Parveen
  • 31
  • 1
  • 9
  • Give us any code. How do you select options? Also I don't get your point about button and a list next to it. What does enable what? – Kirhgoph Jul 12 '17 at 11:30
  • When you select `Self` then only you get the option `General`, which essentially means the HTML DOM gets changed, which results in `StaleElementException` – undetected Selenium Jul 12 '17 at 11:33
  • where is the code ? – Gaurang Shah Jul 12 '17 at 12:02
  • I guess, you are find the element of second drop down before selecting the first dropdown. You have to do the following first select the first dropdown, then find the element to be selected in second dropdown and click – Murthi Jul 12 '17 at 12:13
  • //Select Channel Select oSelectChannel = new Select(driver.findElement(By.id("client"))); oSelectChannel.selectByVisibleText("Insurance Test Client"); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 'code' //Select Category Select oSelectCategory = new Select(driver.findElement(By.xpath("//*[@id='category']"))); oSelectCategory.selectByVisibleText("Product Insurance"); – Parveen Jul 12 '17 at 12:49
  • First i have to select an option from first dropdown. Depending on which option i have selected I will get different options on Dropdown 2(dropdown 2 is empty and is populated only once first dropdown option is selected). @Kirhgoph There's a button called 'Next' at the bottom of the page which is not present on screenshot i provided. sorry for the confusion. – Parveen Jul 12 '17 at 12:52
  • @DebanjanB Yes right. I think that's correct, HTML DOM gets changed deepending on the first selection. How do I handle this please – Parveen Jul 12 '17 at 12:53
  • @Parveen Check my Answer and update me. Thanks – undetected Selenium Jul 12 '17 at 13:14

1 Answers1

3

When you select Insurance Test Client then only you get the option Product Insurance, which essentially means the HTML DOM gets changed, which results in StaleElementException. To avoid that, once we select from the first dropdown, we need to induce some wait for the elements of the second dropdown to render in the HTML DOM. Then we will use Select Class to select an option. Try out the following code block:

//Select Channel 
Select oSelectChannel = new Select(driver.findElement(By.id("client"))); 
oSelectChannel.selectByVisibleText("Insurance Test Client"); 

WebDriverWait wait5 = new WebDriverWait(driver, 10);
wait5.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_a_Category_item")));

//Select Category 
Select oSelectCategory = new Select(driver.findElement(By.xpath("//*[@id='category']"))); 
oSelectCategory.selectByVisibleText("Product Insurance");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Thanks @DebanjanB . It worked. I dint think of using wait until condition. Thanks for your help. – Parveen Jul 12 '17 at 13:21