得到IMG SRC在硒的字符串我有一个谷歌的搜索结果,像如何使用Python
div class="rc"
h3 class="r"
<a href="somelink">
<img id="idFOO" src="data:somethingFOO" style="...">
</a>
我想somethingFOO(或数据:somethingFOO)添加使用Python &硒的字符串。我怎样才能做到这一点?
得到IMG SRC在硒的字符串我有一个谷歌的搜索结果,像如何使用Python
div class="rc"
h3 class="r"
<a href="somelink">
<img id="idFOO" src="data:somethingFOO" style="...">
</a>
我想somethingFOO(或数据:somethingFOO)添加使用Python &硒的字符串。我怎样才能做到这一点?
您感兴趣的不是文字,而是名称为src的属性。所以如果你会这样做,你就不会得到你想要的东西。
find_element_by_id("idFOO").text
如果您的HTML是这样的,
<input id="demo">hello world </input>
然后下面的代码会给你的Hello World
driver.find_element_by_id("demo").text
和下面的代码给你演示
driver.find_element_by_id("demo").get_attribute("id")
所以你的情况,应该是
driver.find_element_by_id("idFOO").get_attribute("src")
尝试:
src = driver.find_element_by_xpath("//div[@class='rc]/h3[@class='r']/a/img").get_attribute("src")
P.S.-使用全部可用的XPath,以避免对实际的DOM重复的冲突。
你是什么意思你想添加,你的意思是你想要获取这个文本并结合字符串或者你想添加一些东西到这个文本? –
我想把“somethingFOO”放在一个字符串中。例如,我希望能够做“返回字符串”,结果是“somethingFOO”。 – csmn123
到目前为止您尝试了什么,哪些方法无效? –