0

I checked the existing answers and it looks like there is no answered question regarding this.

Here it is: selenium 32 bit driver, IE 11, windows 10 64bit. I need to find the elements with the unique static id = 0idMenuTitle or 1idMenuTitle

HTML code

SPAN onmouseover="if(this.style.backgroundColor !=BgColorON)this.style.backgroundColor = BgColorOVER;" onmouseout="if(this.style.backgroundColor !=BgColorON)this.style.backgroundColor = BgColorOFF;" id=1idMenuTitle class=clsMenuTitle style="MAX-WIDTH: none; MIN-WIDTH: auto; MIN-HEIGHT: auto; MAX-HEIGHT: none; BACKGROUND-COLOR: #cccc" minmax_bound="true" width="100%">menu1

I'm using the following:

driver.findElement(By.id("0idMenuTitle")).click();

and have the error

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #\31 idMenuTitle

I'm trying to find with the partial id

driver.findElement(By.cssSelector("[id*=MenuTitle]")).click();

but got

org.openqa.selenium.ElementNotInteractableException: Element is not displayed

I tried with Xpath as well, the same errors. There is no issue if I'm using FF or GC but I must use IE11, no choice. There is no problem to find the elements with id that does not start with numbers...

Any idea please?

Update

I'm trying to find the element by id starting with 0, but selenium shows the error regarding css selector (?) and the id #\31 (???)

Bob Bolden
  • 93
  • 1
  • 2
  • 8
  • A suggestion from: https://stackoverflow.com/a/11284187/384049 is to verify that your browser can find the element directly (without Selenium), e.g. With a number as the first character of an id: `document.querySelector("#2forTandT4two")` which gives me an error in Chromium instead of returning the element or `null`. I don't know if it's a recent change that you can't use an id starting with a number for a css selector. I hat thought this worked... – iJames Aug 19 '22 at 01:06

2 Answers2

1

The reason you are getting Element is not displayed because item is not visible on the page while interacting.Induce WebDriverWait() and wait for elementToBeClickable()

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id*='MenuTitle']"))).click();

Hope this will helps.

KunduK
  • 32,888
  • 5
  • 17
  • 41
  • no, does not help. I got this: TimeoutException: Expected condition failed: waiting for element to be clickable: By.cssSelector: [id*='1idMenuTitle'] (tried for 20 second(s) with 500 milliseconds interval) – Bob Bolden May 30 '20 at 22:52
  • Can you post the HTML of that element? – KunduK May 31 '20 at 00:26
1

You need to consider a couple of things as follows:

  • A WebElement with the value of id attribute as 0idMenuTitle or 1idMenuTitle is not a static id, it's dynamic id, i.e. partial value is assigned when the DOM Tree gets completely loaded. Hence when you use By.id("0idMenuTitle") you face NoSuchElementException.
  • But when you use [id*=MenuTitle] you are seeing ElementNotInteractableException as the locator strategy finds some other element which is not displayed within the HTML DOM.

Solution

To invoke click on the desired element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using id:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("0idMenuTitle"))).click();
    
  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#0idMenuTitle"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='0idMenuTitle']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • sorry, does not work, shows the error: TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id='1idMenuTitle'] (tried for 20 second(s) with 500 milliseconds interval), the same for id and css selector... – Bob Bolden May 31 '20 at 01:43