Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.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
如何在没有ID的Selenium(Java)中单击Javascript按钮?_Javascript_Java_Selenium_Selenium Webdriver_Selenium Ide - Fatal编程技术网

如何在没有ID的Selenium(Java)中单击Javascript按钮?

如何在没有ID的Selenium(Java)中单击Javascript按钮?,javascript,java,selenium,selenium-webdriver,selenium-ide,Javascript,Java,Selenium,Selenium Webdriver,Selenium Ide,我正在用Selenium用Java建立一个程序 在程序开始时,我在程序中使用的Chrome扩展将加载Chrome实例 然后,Chrome导航到该页面,选择所有框,并应单击页面上因扩展而出现的按钮 因此,我试图单击该按钮,但它是一个通过扩展出现在页面上的Javascript按钮。但是,没有可以显式使用的ID 当我检查元件时,我看到的是: <a href="javascript:void(0);" class="selected button-task" style="width: 140p

我正在用Selenium用Java建立一个程序

在程序开始时,我在程序中使用的Chrome扩展将加载Chrome实例

然后,Chrome导航到该页面,选择所有框,并应单击页面上因扩展而出现的按钮

因此,我试图单击该按钮,但它是一个通过扩展出现在页面上的Javascript按钮。但是,没有可以显式使用的ID

当我检查元件时,我看到的是:

<a href="javascript:void(0);" class="selected button-task" 
style="width: 140px; margin-left: 5px; height: 23px;">
<img src="websiteimage.png here" width="20px">Selected Task</a>
谢谢

试试这个:

driver.findElement(By.xpath("//a[contains@class,'selected'] and contains(@class, 'button-task') and contains(text(), 'Selected Task')")).click()
但我点击这个按钮很重要。我该怎么办

您可以使用以下方法单击此按钮:-

WebDriverWait wait = new WebDriverWait(driver,10);
  • 通过.cssSelector()使用
    :-

    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[normalize-space()='Selected Task']"))).click();
    
  • 通过.linkText()使用
    By
    :-

    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[normalize-space()='Selected Task']"))).click();
    
  • 使用.partialLinkText()的
    By
    :-

    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[normalize-space()='Selected Task']"))).click();
    
  • 通过.xpath()使用
    -

    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
    
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//a[normalize-space()='Selected Task']"))).click();
    

我想出了解决办法:

WebElement extensionBox = driver.findElement(By.xpath(".//a[normalize-space()='Selected Task']"));

Actions actionsTwo = new Actions(driver);
JavascriptExecutor jseTwo = (JavascriptExecutor) driver;
actionsTwo.moveToElement(extensionBox).click();
jseTwo.executeScript("arguments[0].click()", extensionBox);

其他答案不起作用,因为它们要么在页面上找不到对象,要么会出现编译错误

嗨,这给了我一个错误,我把它放在原始帖子中,这样你就可以看到它,非常感谢你的帮助!不幸的是,当我写:WebDriverWait wait=WebDriverWait(driver,10)时,它返回了一个语法错误,说:“类型automatedprogram的方法WebDriverWait(ChromeDriver,int)未定义。”这是奇怪的b/c我导入了WebDriverWait和所有东西。有什么想法吗?@user3196126它不应该
ChromeDriver
,您需要传递您之前启动的该函数的引用变量。有些类似于as
ChromeDriver driver=new ChromeDriver();WebDriverWait(driver,10)
哦,不,我就是这么做的,你可以在我的截图中看到我做了什么。我模糊了一些机密细节,但相关的一切都在那里:@user3196126啊,很抱歉应该是
newwebdriverwait(driver,10)感谢您的响应,但最后一个错误是:线程“main”org.openqa.selenium.InvalidArgumentException中的异常:未知错误:元素在点(295564)处不可单击。其他元素将收到单击:。。。。所以基本上它没有被指向正确的点击点。我以前的代码中也有类似的错误,使用了Javascriptexecutor(参见屏幕截图),但我不确定如何将WebDriverWait与Javascriptexecutor集成以修复问题。如果你能帮我,我会非常感激的!