2017-10-29 62 views
2

我在练习Selenium-Webdriver并遇到期望元素(文本)在网页上可见的问题,但我无法点击它。无法点击可见文本但isDisplayed =假

试图检查此元素是否显示或不显示命令行“isDisplayed”,并且控制台返回错误结果。我有点困惑,网页上的文字(请参阅附件中的亮点)是可见的,但为什么不能点击?

在这种情况下,我们该如何对它执行一些操作?请你分享一下你的想法和战略。

非常感谢。

这里是网页: http://www.lazada.com.ph/

Screenshot of the Lazada webpage

我的代码是

System.setProperty("webdriver.chrome.driver", driver_path); 
WebDriver driver = new ChromeDriver(); 

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
    driver.get("https://www.lazada.com.ph/"); 


    //Click on Men's Fashion 
    WebElement loc = driver.findElement(By.xpath("//div[@data-qa-locator='brands-by-category-cats-item']/div[@data-tab-id='2']")); 
    Actions hover = new Actions(driver); 
    hover.moveToElement(loc).click().build().perform(); 

错误日志:

false 

Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-2114, -93.0999984741211) is out of bounds of viewport width (1366) and height (659) 
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' 
System info: host: 'Lorem', ip: '192.168.30.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_65' 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{moz:profile=C:\Users\Lorem\AppData\Local\Temp\rust_mozprofile.y3xzzu5rD0i5, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=XP, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=true, browserVersion=56.0.2, platformVersion=6.3, moz:processID=4724, browserName=firefox, javascriptEnabled=true, platformName=XP}] 
Session ID: 893e64ce-b53c-4bec-9f98-14832e4b7151 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120) 
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586) 
    at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:652) 
    at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:638) 
    at basic.Wait.main(Wait.java:41) 

回答

0

错误说,这一切如下:

org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-2114, -93.0999984741211) is out of bounds of viewport width (1366) and height (659) 
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' 

WebElement您尝试点击是Not Visible,因为它超出了Viewport的。正如你所使用Actions类,因此它显示MoveTargetOutOfBoundsException

你可以考虑下面的代码块滚动内WebElementViewport第一再取JavascriptExecutor帮助点击如下:

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); 
    ChromeOptions options = new ChromeOptions(); 
    options.addArguments("start-maximized"); 
    options.addArguments("disable-infobars"); 
    options.addArguments("--disable-extensions"); 
    WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.lazada.com.ph/"); 
    //Click on Men's Fashion 
    WebElement elem = driver.findElement(By.xpath("//div[@class='c-category-tab c-brands-by-category__tab']/span[@class='c-category-tab__icon c-category-tab__icon_category-fashion']")); 
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem); 
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", elem); 
+0

感谢@DebanjanB,它可以在Chrome和Firefor上使用。我只是有一个小问题,那就是涉及到屏幕尺寸的视口,通常描述的是UI在桌面上的样子,手机,平板电脑的视图;但它也与DEsktop视图上的元素的可见性有关系,请给我看看关于它的一些文章或知识? 此外,使用javascriptexecutor也可以帮助您;但从测试人员的角度来看,有什么建议使用它吗? 谢谢。 – LearningandWorking

+0

这[**'Discussion/QA' **](https://stackoverflow.com/questions/44912203/selenium-web- driver-java-element-is-not-clickable-at-point-36-72-other-el/44916498#44916498)将会清除你的许多查询,让我知道你是否有任何问题。 'UPV ote' **任何答案,如果他们对你有用。 – DebanjanB

0

看来,找你的XPath给予更多然后是一个元素。

请尝试下面的代码。

WebElement loc = driver.findElement(By.xpath("(//div[@data-qa-locator='brands-by-category-cats-item']/div[@data-tab-id='2'])[2]")); 
    Actions hover = new Actions(driver); 
    hover.moveToElement(loc).click().build().perform(); 
+0

非常感谢Ankur帮助我。但似乎这只适用于Chrome,不适用于Firefox。 :( – LearningandWorking

0

问题是你的xpath返回3个元素。如果你尝试下面的代码,你可以看到这一点:

 //Click on Men's Fashion 
     List<WebElement> loc = driver.findElements(By.xpath("//div[@data-qa-locator='brands-by-category-cats-item']/div[@data-tab-id='2']")); 
     System.out.println(loc.size()); 
     int i=1; 
     for(WebElement we :loc) 
     { 
      System.out.println("innerText:" + we.getAttribute("innerText")); 
      System.out.println("getLocation: " + we.getLocation()); 
      System.out.println("getText: " + we.getText()); 
      System.out.println("getSize: " + we.getSize()); 
      if(we.getText().contains("Men's Fashion")) 
      { 
       we.click(); 
      } 

      System.out.println("------------ "+i+" -------------"); 
      i++; 
     } 

输出是:

3 
innerText:Men's Fashion 
getLocation: (-2210, 838) 
getText: 
getSize: (141, 118) 
------------ 1 ------------- 
innerText:Men's Fashion 
getLocation: (178, 838) 
getText: Men's Fashion 
getSize: (141, 118) 
------------ 2 ------------- 
innerText:Men's Fashion 
getLocation: (2567, 838) 
getText: 
getSize: (141, 118) 
------------ 3 ------------- 

如果单击该元素与getText=Men's Fashion它的工作原理。

说实话我不知道为什么xpath返回3个元素。我希望有人给出答案。

+0

感谢您的详细解释。 – LearningandWorking

0

有这么多的方法来达到这个元素。以下更多情侣。

//span[@data-id=4] 
//span[@data-tracking-nav-header="Men's Fashion"]