Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 硒+;WebDriver CSS选择器不工作_Java_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

Java 硒+;WebDriver CSS选择器不工作

Java 硒+;WebDriver CSS选择器不工作,java,selenium,selenium-webdriver,xpath,css-selectors,Java,Selenium,Selenium Webdriver,Xpath,Css Selectors,在Selenium WebDriver中,通过cssSelector进行的findElement不工作或不可访问 我正试着按一个按钮。页面上有许多按钮,具有相同的id和标记,由上一页的脚本自动生成。因此,我无法使用id或任何其他标记访问它。所以我决定选择cssSelector 这是我的密码: import java.util.Random; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import or

在Selenium WebDriver中,通过
cssSelector
进行的findElement不工作或不可访问

我正试着按一个按钮。页面上有许多按钮,具有相同的
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 &amp;&amp; !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>

罗马G
运行1
run2
编辑


请使用以下代码:

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement edit_button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#disablerun.btn.btn-primary")));
edit_button.click();

希望它能帮助你

如果此按钮不在任何iframe中,并且disablerun的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();  
driver.switchTo().frame(WebElement args0);  
//then click on button
如果它位于任何Iframe中,则首先必须使用以下代码将webdriver的焦点切换到该特定Iframe:

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();  
driver.switchTo().frame(WebElement args0);  
//then click on button

使用完整的错误堆栈跟踪更新问题,Selenium Client/WebDriver/WebClient变体和版本共享相关HTMLplease@DebanjanB更新。Plz检查这是否在IFRAME中可能重复?对于您发布的HTML,您的定位器看起来很好。如果你能发一个链接到这个网站,我们可以更容易地找到它。