0

Consider:

<a title="Citrate of Magnesia for Consumers" href="/cdi/citrate-of-magnesia-solution.html">
<b>Citrate of Magnesia</b>

I'm trying to pull data from a medication website, and how do I select all the text in the <b></b> tags?

Because that's the text I want.

I've tried *//a[@b], and it didn't work.

1 Answer 1

2

Assuming you are trying to rely on the preceding a element, use following-sibling, example:

//a/following-sibling::b

Python code:

b = driver.find_element_by_xpath("//a/following-sibling::b")
print(b.text)

If you want multiple b tags having a as a preceding element:

for b in driver.find_elements_by_xpath("//a/following-sibling::b"):
    print(b.text)

Solution provided after chatting:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("http://www.drugs.com/drug-class/laxatives.html?condition_id=&generic=0&sort=rating&order=desc")

# Wait for the table list to load
table = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "table.data-list")))

for b in table.find_elements_by_css_selector("tr td > a[href] > b"):
    print(b.text)
Sign up to request clarification or add additional context in comments.

8 Comments

waits thats what I type in my xpath?
@HalcyonAbrahamRamirez I've added an example python code. Hope that helps.
thanks a lot. will this pull out the text in it though? or wll it just locate the <b></b> tags?
@HalcyonAbrahamRamirez updated the code with a sample of code to get the text
thanks a lot this. xpath things is pretty confusing. care to point me in the right direction to learn it better?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.