按钮点击硒java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16524290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:18:18  来源:igfitidea点击:

Button click selenium java

javaseleniumwebdriverselenium-webdriver

提问by Julien P.

I have a button:

我有一个按钮:

<input type="button" onclick="onOpenSessionClick()" value="Open device access">     

But when I do the command:

但是当我执行命令时:

driver.findElement(By.xpath("//input[@value='Open access device' and @type='submit']")).click();

The click does not. Here is my code:

点击没有。这是我的代码:

if (isElementPresent((By.xpath("//input[@value='Open device access']")))) 
{
    System.out.println("Je suis dans le if");
    Thread.sleep(2000);
    driver.findElement(By.xpath("//input[@value='Open device access' and @type='submit']")).click();
    System.out.println("Je suis dans le if et jai open");
    Thread.sleep(5000);
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div/p/span")));                       
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div[2]/input")));                     
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div[2]/input[2]")));                      
    System.out.println("Je suis dans le if et je cherche");
}

回答by Ievgen

typein your case is button, not submit.

type在你的情况下是button,不是submit

Try this one //input[@value='Open device access']or //input[@value='Open device access' and @type='button']

试试这个//input[@value='Open device access']//input[@value='Open device access' and @type='button']

回答by Omkar

You can try this one too as CSS Selector

你也可以试试这个作为 CSS Selector

driver.findElement(By.cssSelector("input[type='button'][value='Open device access']")).click();

or

或者

driver.findElement(By.cssSelector("input[type='button']")).click();

回答by Ritesh Baid

Just try the below code for click

只需尝试以下代码 click

selenium.focus("name=Valuation"); //name of button
selenium.click("Valuation"); //pass that name to click

回答by Sagar Ajmire

You can check whether this button is on frame or not. If it is, then you need to switch to the frame and then locate and click.

您可以检查此按钮是否在框架上。如果是,那么您需要切换到框架,然后定位并单击。

Hope this will help you.

希望这会帮助你。