The html looks something like this:
<p>
sometext1
<br>
sometext2
<br>
sometext3
</p>
I would like to extract all the text between the paragraph tags, including the <br>
tags.
I tried to use storeText
function, but it stores only the text, without the tags.
I could store the entire HTML sour开发者_如何学Cce and then extract what I need in Perl, but I was wondering if there is a way to store a block of HTML code using a specific xpath (e.g. store the HTML code for the third table in the webpage inside a variable).
innerHTML
i will try with document.getElementById('id').innerHTML
you could use a getEval() with Javascript that return the innerHTML of the element. You'll have to find it in javascript, though
@Tarun: I would if I could man....
@Grooveek: Thanks man, that worked. I used:
storeEval | window.document.getElementsByTagName("p").item(9).innerHTML | p
This saved the content of the 9th paragrah in the variable p
.
I had to use getElementsByTagName
because the tags had no id's.
For more accuracy, one could use getElementById
function insted:
storeEval | window.document.getElementById("id of element").innerHTML | p
Hope this will help other people too. Thanks again.
I suggest this:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("url")
element = driver.find_element_by_tag_name("p")
text = element.text
But keep in mind if you are dealing with text-boxes, you can't use .text
; it returns None
. In that case you should use .get_attribute("value")
, and when ever you are unable to catch what you want, you can use .get_attribute("innerHTML")
.
getAttribute("innerHTML"); works for me
I propose to find it by a class name, not all objects have it's Id.
storeEval | window.document.getElementsByClassName('*classname*')[0].innerHTML; | HTMLContent
number 0 will return first occurence. If there is more than one element, choose proper number, or get the number of class occurencies by .length
storeEval | window.document.getElementsByClassName('*classname*').length; | ClassCount
精彩评论