我有一个谷歌搜索结果,大致如下:
div class="rc"
h3 class="r"
<a href="somelink">
<img id="idFOO" src="data:somethingFOO" style="...">
</a>
我想使用Python和Selenium将somethingFOO(或data:somethingFOO)添加到字符串中。我该怎么做?
我有一个谷歌搜索结果,大致如下:
div class="rc"
h3 class="r"
<a href="somelink">
<img id="idFOO" src="data:somethingFOO" style="...">
</a>
我想使用Python和Selenium将somethingFOO(或data:somethingFOO)添加到字符串中。我该怎么做?
你感兴趣的不是文本,而是一个名为“src”的属性。因此,如果你像这样做,你将得不到你想要的结果。
find_element_by_id("idFOO").get_attribute("src")
如果你的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")
driver.find_element_by_xpath("//*[contains(@id,'idF')]")
- Gaurang Shah尝试:
src = driver.find_element_by_xpath("//div[@class='rc]/h3[@class='r']/a/img").get_attribute("src")
P.S. - 使用完整的xpath以避免在实际DOM上出现重复冲突。