对于附加的程序文件,我面临着附加类的问题。该行代码失败
CSS:
<a class="btn btn--action btn--block" data-dojo-attach-point="sign_in_button" data-dojo-attach-event="onclick: _onSignInClicked" tabindex="2">Verify</a>
因此我的代码是:
drive.findElement( By.cssSelector(".btn.btn--action.btn--block")).click();
但我一直在控制台中收到此错误:
*** Element info: {Using=xpath, value=//a[contains(@data-dojo-attach-point,'sign_in_button')]}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:476)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at WebFlock.main(WebFlock.java:23)
CODE:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebFlock {
public static void main(String[] args) throws Exception {
//open flock.co on chrome
WebDriver drive;
String url = "https://flock.co/?";
System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
drive = new ChromeDriver();
drive.get(url);
Thread.sleep(5000);
//find the email text box, enter email and click submit
drive.findElement(By.cssSelector("Input[placeholder ='Enter your work email']")).sendKeys("me@farzanshaikh.com");
drive.findElement(By.cssSelector("button[type='submit']")).click();
Thread.sleep(8000); //wait for loader to resolve
drive.findElement( By.cssSelector(".btn.btn--action.btn--block")).click();
}
}
答案 0 :(得分:1)
请尝试以下代码:
drive.switchTo().frame(drive.findElement(By.cssSelector("#dijit__WidgetsInTemplateMixin_0 > iframe")));
WebDriverWait wait = new WebDriverWait(drive,15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a.btn")));
drive.findElement( By.cssSelector("a.btn")).click();
<强>解释强>
元素存在于<iframe>
;因此,为了获得该元素,我们首先需要使用iframe
方法切换到.switchTo.frame()
。