如何在没有 ID (Java) 的情况下单击 Selenium 中的 Javascript 按钮?
How to click on Javascript button in Selenium with no ID (Java)?
我正在使用 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>
与我可以单击的其他内容不同,没有类型(复选框、按钮等)或我可以查找的特定 ID。但重要的是我点击这个按钮。我应该怎么办?
当我使用这个时出现这个错误:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector:
Unable to locate an element with the xpath expression //a[contains@class,'selected'] and contains(@class, 'repost-selected button-task') and contains(text(), 'Repost Selected') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document':
The string '//a[contains@class,'selected'] and contains(@class, 'repost-selected button-task') and contains(text(), 'Repost Selected')' is not a valid XPath expression.
谢谢!
试试这个:
driver.findElement(By.xpath("//a[contains@class,'selected'] and contains(@class, 'button-task') and contains(text(), 'Selected Task')")).click()
But it is important I click this button. What should I do?
您可以使用以下方法点击此按钮:-
WebDriverWait wait = new WebDriverWait(driver,10);
使用 By.cssSelector()
:-
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
使用 By.linkText()
:-
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
使用 By.partialLinkText()
:-
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
使用 By.xpath()
:-
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);
其他答案无效,因为它们要么在页面上找不到对象,要么会给出编译错误
我正在使用 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>
与我可以单击的其他内容不同,没有类型(复选框、按钮等)或我可以查找的特定 ID。但重要的是我点击这个按钮。我应该怎么办?
当我使用这个时出现这个错误:
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector:
Unable to locate an element with the xpath expression //a[contains@class,'selected'] and contains(@class, 'repost-selected button-task') and contains(text(), 'Repost Selected') because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document':
The string '//a[contains@class,'selected'] and contains(@class, 'repost-selected button-task') and contains(text(), 'Repost Selected')' is not a valid XPath expression.
谢谢!
试试这个:
driver.findElement(By.xpath("//a[contains@class,'selected'] and contains(@class, 'button-task') and contains(text(), 'Selected Task')")).click()
But it is important I click this button. What should I do?
您可以使用以下方法点击此按钮:-
WebDriverWait wait = new WebDriverWait(driver,10);
使用
By.cssSelector()
:-wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.selected.button-task"))).click();
使用
By.linkText()
:-wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Selected Task"))).click();
使用
By.partialLinkText()
:-wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Selected Task"))).click();
使用
By.xpath()
:-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);
其他答案无效,因为它们要么在页面上找不到对象,要么会给出编译错误