<div class="toolbar">
<table>
<tbody>
<tr>
<td class="text-button menu-button-active" itemid="sdfsgsg.0978" title="Actions" norap="">
Actions
<img src="../common/ads.gif">
</td>
</tr>
</tbody>
</table>
img
是td的子标记,并且类属性值是动态的。
错误日志:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //div[@class='toolbar']//td[text()='Actions']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 52.9.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:processID: 9268, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, specificationLevel: 0, timeouts: {implicit: 0, page load: 300000, script: 30000}}
Session ID: 0f4515d3-fad9-4c70-b50a-27f6c9abd249
*** Element info: {Using=xpath, value=//div[@class='toolbar']//td[text()='Actions']}
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:548)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:424)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
at PCS.PCS_TEst.BaseClass.CreateColor(BaseClass.java:100)
答案 0 :(得分:0)
Xpath是:
//div[@class='toolbar']//td[text()='Actions']
答案 1 :(得分:0)
根据HTML,您可以使用以下 xpath :
//td[@class='text-button menu-button-active' and @title='Actions'][normalize-space()='Actions']
此错误消息...
org.openqa.selenium.NoSuchElementException: Unable to locate element: //div[@class='toolbar']//td[text()='Actions']
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
...表示 GeckoDriver 无法通过定位器策略将元素定位为//div[@class='toolbar']//td[text()='Actions']
。
根据您共享的HTML,您需要通过 WebDriverWait 诱导 waiter ,并且可以使用以下解决方案:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[@class='text-button menu-button-active' and @title='Actions'][normalize-space()='Actions']"))).click();
但是您的主要问题是正在使用的二进制版本之间的不兼容性,如下所示:
解决方案:
@Test
。答案 2 :(得分:0)
如果只需要“ td”元素,请使用CSS来获取它:
WebElement actionsElement = driver.findElement(By.cssSelector("div.toolbar td"));
actionsElement.click();
如果要单击内部图像元素,则使用like:
WebElement imageElement = driver.findElement(By.cssSelector("div.toolbar td img"));
imageElement.click();
希望对您有帮助。
使用xpath: 对于动作“ td”元素:
//td[@title='Actions']
对于img内部元素:
//td[@title='Actions']//img
答案 3 :(得分:0)
如果 img 是 td 的子元素,请尝试
//td[@class="text-button menu-button-active"]//img
如果 img 是 td 的同级元素,请尝试
//td[@class="text-button menu-button-active"]/following-sibling::img
希望这会有所帮助:)
答案 4 :(得分:0)
由于文本中有空格,因此您需要使用normalize-space
清除它们:
//div[@class='toolbar']//td[normalize-space(.)='Actions']