在Selenium WebDriver中,findElement By cssSelector
无效或无法访问。
我试图按下按钮。页面上有许多按钮,具有相同的id
和标签,由前一页的脚本自动生成。因此,我无法使用id
或任何其他标记访问它。所以我决定去cssSelector
。
这是我的代码:
import java.util.Random;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class mrc {
public static void main(String[] args) {
System.out.flush();
//Create firefoxDriver Object.
System.setProperty("webdriver.gecko.driver", "C:\\eclipse\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
// LOGIN
driver.get("http://ec2#####-west-2.compute.amazonaws.com/login");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/div[2]/div/input")).sendKeys("admin@admin.com");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/div[3]/div/input")).sendKeys("admin");
driver.findElement(By.xpath("/html/body/div[3]/div[1]/div[2]/div/form/button[1]")).click();
// JUDGEPANEL
// Navigate to judgepanel
driver.navigate().to("http://ec2#####-west-2.compute.amazonaws.com/judgepanel");
// select event
Select drpdwn = new Select(driver.findElement(By.id("selector")));
drpdwn.selectByValue("70");
//drpdwn.selectByIndex(drpdwn.getOptions().size()-1);
driver.findElement(By.xpath("/html/body/div[3]/div[2]/div[2]/div[1]/div/div[4]/div[2]/button")).click();
// UPTO THIS PART IT WORKS FINE
// enter scores
driver.findElement(By.cssSelector("button#disablerun.btn.btn-primary")).click();
System.out.println("Button 1 Pressed");
}
}
错误是:
1525438082224 geckodriver INFO geckodriver 0.20.1
1525438082242 geckodriver INFO Listening on 127.0.0.1:39642
1525438083119 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\karan\\AppData\\Local\\Temp\\rust_mozprofile.fUb9UrnMUqq8"
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
1525438084292 Marionette INFO Enabled via --marionette
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
1525438086674 Marionette INFO Listening on port 63161
1525438086758 Marionette WARN TLS certificate errors will be ignored for this session
May 04, 2018 6:18:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Unable to read VR Path Registry from C:\Users\karan\AppData\Local\openvr\openvrpaths.vrpath
JavaScript warning: http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript error: http://ec2#####-west-2.compute.amazonaws.com/js/all.js, line 5831: TypeError: gapi.signin is undefined
JavaScript warning: http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: button#disablerun.btn.btn-primary
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z'
System info: host: 'KARAN-VAIO', ip: '*.*.*.*', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '10'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 1584, moz:profile: C:\Users\karan\AppData\Loca..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: b3b62974-ae5d-4885-956f-5ab2edf03dc9
*** Element info: {Using=css selector, value=button#disablerun.btn.btn-primary}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311)
at mrc.main(mrc.java:51)
我已经尝试过xPath了。它也不起作用。如何解决此错误?
按钮的HTML是:
<div ng-if="!isLocked && !Userlock" class="row ng-scope" ng-repeat="entrance in entranceList">
<div class="col-xs-12 col-sm-12">
<div class="row">
<div class="col-xs-12 col-sm-3 ng-binding">
Roma G
</div>
<div class="col-xs-12 col-sm-3">
<!-- run1 <input type="number" placeholder="Run 1 score" ng-model=entrance.run1> -->
run1 <b><span ng-bind="entrance.run1" class="ng-binding"></span></b>
</div>
<div class="col-xs-12 col-sm-3">
<!-- run2 <input type="number" placeholder="Run 2 score" ng-model=entrance.run2> -->
run2 <b><span ng-bind="entrance.run2" class="ng-binding"></span></b>
</div>
<div class="col-xs-12 col-sm-3">
<!-- <button class="button" ng-click=saveEntraceScore(entrance)> Save</button> -->
<button id="disablerun" class="btn btn-primary" ng-click="EditEntraceScore(entrance)"> Edit
<!-- <div ng-if="isbattleTree === true" class="">
<i class="fa fa-lock"></i></div> -->
</button>
</div>
</div>
<hr>
<div class="col-xs-12 col-sm-6">
</div>
</div>
</div>
答案 0 :(得分:0)
使用以下代码:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement edit_button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#disablerun.btn.btn-primary")));
edit_button.click();
希望它可以帮到你!
答案 1 :(得分:0)
如果此按钮不在任何 iframe 且禁用运行的ID是唯一的,那么此代码应该适合您:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.id("disablerun")));
driver.findElement(By.id("disablerun")).click();
如果 ID 不是唯一,那么您可能想尝试以下代码:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Edit')]")));
driver.findElement(By.xpath("//button[contains(text(),'Edit')]")).click();
如果它在任何Iframe中,那么首先你必须使用以下代码将webdriver的焦点切换到特定的iframe:
driver.switchTo().frame(WebElement args0);
//then click on button