我无法使用 selenium webdriver 选择元素。我正在尝试获取“学生书”,然后单击属于它的开始按钮。有几本书具有相似的标题,特别重要的是要准确获得这本书。使用 id 来查找元素不起作用,因为它们正在生成并且看起来并不总是相同。
html 如下所示:
<table style="margin: 0; width: 100%;">
<tbody>
<tr>
<td><img src="/image/db/prod/pic.jpg" alt=""></td>
<td style="padding: 0; vertical-align: top; position: relative;">
<h3 class="top0" style="width: 200px;" id="crp_title_254">Student's Book</h3>
<hr><a class="linkButton orangeLinkButton bold" href="some_link">Start</a>
<a class="btn_crpdel" id="btn_crpdel_254"><img class="style-del" src="/styles/pic/style-del.png" alt="-" style="width: 23px; height: 23px;"></a>
</td>
</tr>
</tbody>
</table>
python 部分看起来像这样,material 变量保存值“Student's Book”:
try:
#material = "Student's Book"
start_link = self.browser.find_element(By.XPATH, "//h3[text()='" + material + "']/following::a[contains(text(), launch_button[self.INTERFACE_LANGUAGE])]")
start_link.click()
except Exception as e:
Exceptionhandling...
运行上述代码时出现以下异常:
InvalidSelectorException('Given xpath expression "//h3[text()=\'Student Book\'s\']/following::a[contains(text(), launch_button[self.INTERFACE_LANGUAGE])]" is invalid: SyntaxError: The expression is not a legal expression.', None, None)
如果其中没有单引号,则整个事情都可以正常工作。有人知道我在做什么错吗?
提前致谢!