Selenium Java: 侧边栏菜单导航 – > 无法选择无序列表菜单项

huangapple go评论49阅读模式
英文:

Selenium Java : Side bar menu navigation - > Unable Select the un-ordered list menu item

问题

我正在尝试一种选择侧边导航栏菜单项的替代方法。这些元素位于一个 iframe 中。我已经切换到了 iframe 并尝试选择侧边菜单中的一个项目,以便可以选择相应的导航项。这些菜单位于一个 <ul> 标签中。您会注意到,默认情况下会选择第一个菜单/列表项。我正在尝试选择"Customers"。我正在使用 Selenium WebDriver 来进行Java编程。请查看附带HTML的截图。

我已经尝试了以下方法,以及其他一些没有成功的XPath:

  1. XPath-//*[@class=&#39;crm_sitemap_catalog_item&#39; and contains(text(),&#39;Customers&#39;)]
  2. XPath-//*[@id=&quot;catalog&quot;]/ul/li[5]/div[2]/div
  3. 完整的XPath- //html/body/div[1]/div[1]/ul/li[5]/div[2]/div
  4. CSS选择器-#catalog &gt; ul &gt; li.crm_sitemap_catalog_item.selected &gt; div.body1 &gt; div

这是一个返回的错误消息示例:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@class=&#39;crm_sitemap_catalog_item&#39; and contains(text(),&#39;Customers&#39;)]&quot;}

使用替代的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 &lt;ul&gt; 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:

  1. Xpath-//*[@class=&#39;crm_sitemap_catalog_item&#39; and contains(text(),&#39;Customers&#39;)]
  2. Xpath-//*[@id=&quot;catalog&quot;]/ul/li[5]/div[2]/div
  3. Full Xpath- //html/body/div[1]/div[1]/ul/li[5]/div[2]/div
  4. CSS Selector-#catalog &gt; ul &gt; li.crm_sitemap_catalog_item.selected &gt; div.body1 &gt; div

This is one example of the error message that is returned:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@class=&#39;crm_sitemap_catalog_item&#39; and contains(text(),&#39;Customers&#39;)]&quot;}

I would get the same error using alternative xpath values.

Selenium Java: 侧边栏菜单导航 – > 无法选择无序列表菜单项

答案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, &quot;//div[text()=&#39;Customers&#39;]/parent::div/parent::li&quot;)))
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=&#39;Customers&#39;])).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 &quot;chrome&quot;:
                objectAction.moveToElement(onWebElement).perform();
                objectAction.moveToElement(onWebElement).click().perform();
            break;
        case &quot;firefox&quot;:
            ((RemoteWebDriver) 
            mydriver).executeScript(&quot;arguments[0].scrollIntoView();&quot;, onWebElement);
            objectAction.moveToElement(onWebElement).build().perform();
            objectAction.click();
            objectAction.perform();
                break;
           
    }
}

huangapple
  • 本文由 发表于 2020年7月29日 02:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63140711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定