英文:
How to click the element if the value of the id attribute starts with numbers using Selenium in IE11
问题
我检查了现有的答案,看起来关于这个问题没有被回答过。
以下是您要的翻译:
在这里:selenium 32位驱动程序,IE 11,Windows 10 64位。
我需要找到具有唯一静态id = 0idMenuTitle
或1idMenuTitle
的元素。
HTML代码
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</SPAN
我正在使用以下内容:
driver.findElement(By.id("0idMenuTitle")).click();
但出现错误
org.openqa.selenium.NoSuchElementException: 无法找到具有css选择器 == # idMenuTitle 的元素
我试图通过部分id查找
driver.findElement(By.cssSelector("[id*=MenuTitle]")).click();
但出现
org.openqa.selenium.ElementNotInteractableException: 元素不可交互
我也尝试了XPath,同样出现错误。
如果我使用FF或GC没有问题,但我必须使用IE11,没有选择。
对于不以数字开头的id,没有问题...
有什么想法吗?
更新
我正在尝试通过以0开头的id找到元素,但是selenium显示与css选择器有关的错误(?),id为#\31(?)
英文:
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</SPAN
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 == # 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 (???)
答案1
得分: 1
因为项目在进行交互时页面上的元素不可见,所以出现了“Element is not displayed”的情况。可以使用WebDriverWait()
函数,并等待elementToBeClickable
()方法:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("[id*='MenuTitle']"))).click();
希望这对你有帮助。
英文:
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.
答案2
得分: 1
你需要考虑以下几点:
- 具有 id 属性值为 0idMenuTitle 或 1idMenuTitle 的 WebElement 并不是一个静态 id,它是动态 id,即在 DOM 树 完全加载时分配部分值。因此,当你使用
By.id("0idMenuTitle")
时会遇到 NoSuchElementException。 - 但是,当你使用
[id*=MenuTitle]
时,你会看到 ElementNotInteractableException,因为 定位策略 找到了一些在 HTML DOM 内未显示的其他元素。
解决方法
要在所需元素上触发点击操作,你需要使用 WebDriverWait 并使用 elementToBeClickable()
,你可以使用以下任一定位策略:
-
使用 id:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("0idMenuTitle"))).click();
-
使用 cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#0idMenuTitle"))).click();
-
使用 xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='0idMenuTitle']"))).click();
英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论