【发布时间】:2020-06-07 02:54:35
【问题描述】:
我一直在为这个 selenium 项目而苦苦挣扎,并一直在寻找与这个问题无关的旧参考资料或帖子。我承认我是 xpath 和 selenium 的新手,所以希望它可能像修复我的语法一样简单。如果这有什么不同,我在 lubuntu 19.10 上使用 python 3.6.9。
我正在尝试访问的html元素:
<div class="NfvXc">
<textarea aria-label="Write a caption…" autocomplete="off" autocorrect="off"
class="_472V_" placeholder="Write a caption…"></textarea>
</div>
我试过的代码:
add_text = browser_object.find_element_by_xpath("//textarea[contains(@aria-label,'Write a caption...')]")
add_text = browser_object.find_element_by_xpath("//div[contains(text(),'Write a caption...')]")
我在尝试不同的 xpath 时得到的错误消息:
error in process_image(): Message: Given xpath expression "//*div[contains(text(),'Write a caption...')]" is invalid: SyntaxError: The expression is not a legal expression.
我检查了 firefox 以尝试捕获 xpath,显示为:
/html/body/div[1]/section/div[2]/section[1]/div[1]/textarea
虽然我不确定如何调整我的 xpath 以找到该元素,但显然。
【问题讨论】:
-
您正在使用
//*div[contains(@placeholder,'Write a caption...')],请使用//div[contains(@placeholder,'Write a caption...')],或者如果您确定页面中没有其他元素包含占位符Write a caption...//*[contains(@placeholder,'Write a caption...')],请使用它
标签: html python-3.x selenium-webdriver xpath