尝试单击其父代/祖先。例如,如果您有
//a[text()='link of your text']
并且您收到 javascript 错误,请尝试:
//a[text()='link of your text']/parent::*
尝试以下代码 - 它适用于 Salesforce Lightning UI 屏幕:
WebElement element = driver.findElement(By.xpath("your xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
测试后发现可以用,分享一下。
要单击以下链接,请使用
WebDriverWait
和 elementToBeClickable
,然后使用以下 xpath 单击该链接。
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@title='Country']/span[@class='slds-truncate'][contains(.,'Country')]"))).click();
对于那些尝试使用 Python 的人,您可以这样做:
>>> button_link = driver.find_element_by_xpath('''//*[@id="tab-23"]/slot''')
>>> driver.execute_script("arguments[0].click();", button_link )
尝试 JavaScript。它对我有用
WebElement element = driver.findElement(By.xpath("strPath"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
使用 SalesForce,我必须选择两个更高的父级别,单击
one-app-nav-bar-item-root
,而不是锚标记,这一开始并不直观。
要单击我的帐户选项卡,我使用了
//one-app-nav-bar-item-root/a/span[text()='Accounts']/parent::*/parent::*
/**
* <p>This method will navigate to a tab open in the top of SalesForce using the tabs name.</p>
* @param tabName The text displayed on the tab. Not a URL.
* @author <a href="https://stackoverflow.com/users/9889584/sjb">Shawn J Burke</a>
*/
public void navigateOneAppBarItem(String tabName) {
String xPath = String.format("//one-app-nav-bar-item-root/a/span[text()='%s']/parent::*/parent::*", tabName);
System.out.println(xPath);
WebElement oneBarTab = this.getWebDriver().findElement(By.ByName.xpath(xPath));
oneBarTab.click();
}