I need to scrape a website with a structure like this:
<table>
<tbody>
<tr>
<td>
<p>
<a href="href1">Text1</a>
<a href="href2">Text2</a>
<a href="href3">Text3</a>
. . .
. . .
</p>
<p> . . . </p>
# More <p> elements with <a> as childrens
</td>
<td> . . . </td
# More <td> elements
</tr>
<tr> . . . </tr>
</tbody></table>
I want to get these <a>
elements (text and href). The code that I have is:
elem = driver.find_element_by_xpath("//table")
elems = elem.find_elements_by_xpath("./p/a")
print(len(elems))
for i in elems:
print(i.text)
time.sleep(4)
The length of elems prints 0
so basically the elements are not found. I have tried ./a
alone also instead of ./p/a
but same result. I am new to selenium so please help.
try to use xpath
for getting all a tag elements
//table//tbody//child::p[1]//a
for getting specific a tag element
//table//tbody//child::p[1]//a[1]
//table//tbody//child::p[1]//a[2]
//table//tbody//child::p[1]//a[3]
You can find all the texts by using the code:
elements = driver.find_elements_by_xpath("//a[contains(@href,'href')]")
for element in elements:
print(element.text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.