我正在练习Selenium-Webdriver并遇到网页上可见元素(文本)可见的问题,但我无法点击它。
尝试检查此元素是否通过命令行“isDisplayed”显示,并且控制台返回false结果。我有点困惑,网上的文字(请看附件中的重点)是可见的,但为什么不能点击?
在这种情况下,我们如何对其执行某些操作?请你分享你的想法和策略。
非常感谢你。
这是网页: http://www.lazada.com.ph/
我的代码是
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)
错误日志:
f [(h test) (h test2)]
答案 0 :(得分: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();
答案 1 :(得分: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个元素。我希望有人给出答案。
答案 2 :(得分:0)
有很多方法可以达到这个元素。下面再说几句。
//span[@data-id=4]
//span[@data-tracking-nav-header="Men's Fashion"]
答案 3 :(得分: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
您可以考虑使用以下代码块来首先滚动 WebElement
中的 Viewport
,然后获取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);