2017-01-10 9 views
0

Mein Code auf dem Wert aus dem Dropdown-Menü klicken soll, habe ich diesen Code:Wie schreibt man Mouseover mit PageFactory

WebElement element = driver.findElement(By.xpath("//a[text()='Product Category']")); 
Actions action = new Actions(driver); 
action.moveToElement(element).perform(); 
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='iMacs']")), 500); 
WebElement subElement = driver.findElement(By.xpath("//a[text()='iMacs']")); 
action.moveToElement(subElement); 
action.click(); 
action.perform(); 

Ich habe versucht, den Code neu zu schreiben, und ich schreibe mit PageFactory:

WebElement element = mouse_over_product_category; 
Actions action = new Actions(driver); 
action.moveToElement(element).perform(); 
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='iMacs']")), 500); 
WebElement subElement = link_iMacs; 
action.moveToElement(subElement); 
action.click(); 
action.perform(); 

Mein Fehler ist:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document 

Kann mir jemand helfen, wie man schreibt. Ich bin ein Anfänger.

Antwort

0
WebElement element=driver.findElement(By.xpath("//a[text()='Product Category']"));   
String mouseOver = "var evObj = document.createEvent('MouseEvents');" + 
          "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" + 
          "arguments[0].dispatchEvent(evObj);"; 


    ((JavascriptExecutor)driver).executeScript(mouseOver, element); 

Dieser ist mit JavaScript

+0

Gibt dies an me: Kann das Symbol 'GlobalVars' nicht auflösen –

+0

Das war nur meine lokale Instanz von WebDriver. Benutze das aktualisierte. Sollte arbeiten. –

+0

Ihr Code funktioniert, aber öffnet das erste Element im Dropdown-Menü, und ich möchte auf ein anderes klicken. –

0

Es sind mehr als diese zwei Möglichkeiten, dies zu tun ..

versuchen erreichen, was Sie brauchen, um diese unter Verwendung von:

//Conventional Way- Identifying the Select Menu Box and sending the text desired to select either by Text or Index or by value 



    WebElement elDropDownItem = driver.findElement(By.xpath("")); 

       if (elDropDownItem != null) { 
        Select comboBox = new Select(elDropDownItem); 

//Either of these 3 should work SelectBy-- should work 
         comboBox.selectByVisibleText(MenuItemText); 
         break; 
      //OR   
         comboBox.selectByIndex(Integer 
           .parseInt(MenuItemIndex)); 
         break; 
      //OR   
         comboBox.selectByValue(MenuItemValue); 
         break; 
        } 

Ein anderer Weg, :

//Clicking the down arrow and then clicking on the Select Item you desire 
      WebElement element= driver.findElement(By.xpath("downArrow")); 
      element.click(); 

WebElement element1= driver.findElement(By.xpath("selectMenuItem")); 
      element1.click();