英文:
Selenium Java : Side bar menu navigation - > Unable Select the un-ordered list menu item
问题
我正在尝试一种选择侧边导航栏菜单项的替代方法。这些元素位于一个 iframe 中。我已经切换到了 iframe 并尝试选择侧边菜单中的一个项目,以便可以选择相应的导航项。这些菜单位于一个 <ul>
标签中。您会注意到,默认情况下会选择第一个菜单/列表项。我正在尝试选择"Customers"。我正在使用 Selenium WebDriver 来进行Java编程。请查看附带HTML的截图。
我已经尝试了以下方法,以及其他一些没有成功的XPath:
- XPath-
//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]
- XPath-
//*[@id="catalog"]/ul/li[5]/div[2]/div
- 完整的XPath-
//html/body/div[1]/div[1]/ul/li[5]/div[2]/div
- CSS选择器-
#catalog > ul > li.crm_sitemap_catalog_item.selected > div.body1 > div
这是一个返回的错误消息示例:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]"}
使用替代的XPath值会得到相同的错误。
英文:
I am trying an alternative way to select a menu item in the sidebar navigation. These elements are within an iframe. I have switched to the iframe and am trying to select an item from the side menu in order for the respective navigational items to be displayed for selection. These menus are sitting in a <ul>
tag. You will notice that by default the very first menu/list item is selected. I am trying to select Customers. I am using Selenium webDriver for Java. See the screenshot with the HTML.
I have tried the following already, and other xpaths
with no success:
- Xpath-
//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]
- Xpath-
//*[@id="catalog"]/ul/li[5]/div[2]/div
- Full Xpath-
//html/body/div[1]/div[1]/ul/li[5]/div[2]/div
- CSS Selector-
#catalog > ul > li.crm_sitemap_catalog_item.selected > div.body1 > div
This is one example of the error message that is returned:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='crm_sitemap_catalog_item' and contains(text(),'Customers')]"}
I would get the same error using alternative xpath values.
答案1
得分: 1
请使用以下的 XPath
:
wait = WebDriverWait(driver, 20)
LiOption = wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Customers']/parent::div/parent::li")))
action.move_to_element(LiOption).click(LiOption).perform()
运行它,如果出现任何错误,请告诉我。
英文:
Please use the below XPath
wait = WebDriverWait(driver, 20)
LiOption = wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Customers']/parent::div/parent::li")))
action.move_to_element(LiOption).click(LiOption).perform()
Run it and let me know if you get any errors.
答案2
得分: 1
"你提到的要访问的元素位于一个iFrame中,所以请确保首先切换到iFrame。
driver.switchTo().frame() //frame()是一个重载方法,所以你可以使用适合你的方式。
然后,请确保等待iFrame加载完成,或者等待下面的元素加载完成:
driver.findElement(By.cssSelector("div[title='Customers']")).click();
"
英文:
As you have mentioned the element you are trying to access is inside an iFrame so make sure you switch to iFrame first.
driver.switchTo().frame() //frame() is overloaded method so you can use which suits you.
Then make sure you wait for iFrame/ or wait for below element to load
driver.findElement(By.cssSelector(div[title='Customers'])).click();
答案3
得分: 1
我已决定采用顶部菜单导航方法,其工作如下:
public void PerformItemNavigation(WebElement onWebElement, WebDriver mydriver) {
Actions objectAction = new Actions(mydriver);
Capabilities cap = ((RemoteWebDriver) mydriver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
switch (browserName) {
case "chrome":
objectAction.moveToElement(onWebElement).perform();
objectAction.moveToElement(onWebElement).click().perform();
break;
case "firefox":
((RemoteWebDriver) mydriver).executeScript("arguments[0].scrollIntoView();", onWebElement);
objectAction.moveToElement(onWebElement).build().perform();
objectAction.click();
objectAction.perform();
break;
}
}
希望这对你有帮助。
英文:
I have decided to follow the top menu navigation approach and it works as follows:
public void PerformItemNavigation(WebElement onWebElement , WebDriver mydriver) {
Actions objectAction = new Actions(mydriver);
Capabilities cap = ((RemoteWebDriver) mydriver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
switch (browserName){
case "chrome":
objectAction.moveToElement(onWebElement).perform();
objectAction.moveToElement(onWebElement).click().perform();
break;
case "firefox":
((RemoteWebDriver)
mydriver).executeScript("arguments[0].scrollIntoView();", onWebElement);
objectAction.moveToElement(onWebElement).build().perform();
objectAction.click();
objectAction.perform();
break;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论