0

I'm trying to web scrape the person's name and company. This is what I've tried.

    <div id="viewcontact">
        <table width="100%">
            <tbody><tr>
                <td style="display: inline-block; width: 30%">
                    <div class="formsection_light" style="margin-top:-8px;background:#eaeaea;">
                        <div style="padding-bottom:10px;">
                            <div class="left">
                                <h1>Company Name</h1>
                                    <p class="f16">Person's Name</p>
                            <div class="theme">
                                    Person's Name                           
                            </div>
                        </div>
                        <div class="right" style="margin-top:5px;">

    driver.find_element_by_xpath('//h1[@class="left"]')
    driver.find_element_by_class_name("f16")

And the output was nothing, no errors just didn't scrape anything

MattDMo
  • 100,794
  • 21
  • 241
  • 231
ApexFanta
  • 1
  • 1
  • Okay, so..... what happened? Did those function calls return anything? Were there any errors or tracebacks? Did you use Chrome's Developer Tools to get the full XPath? Please [edit] your question and create a [mre] so we can replicate the behavior you're seeing. That includes giving us an actual URL, or posting enough of the HTML (**in text**, not as images) to test possible solutions. Please see [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/ask) from the [intro tour](https://stackoverflow.com/tour). – MattDMo Aug 22 '21 at 18:32
  • Excuse my negligence, it's my first time on this site. I'm not sure what you meant by Chrome's Developer Tools to get the full xpath – ApexFanta Aug 22 '21 at 19:14
  • Please Google the term to learn about what they are and how to use them. See [this answer](https://stackoverflow.com/a/42194160/1426065) for instructions on how to copy the full XPath as well as the shorter one. – MattDMo Aug 22 '21 at 19:22
  • Matt, I updated the code to driver.find_element_by_xpath('/html/body/div[6]/div[3]/div[5]/div[2]/div[2]/div[3]/div/table/tbody/tr/td[1]/div[1]/div[1]/div[1]/h1') which is what I got when I copied through Chrome Developer tools. The code it's outputting is – ApexFanta Aug 22 '21 at 19:33
  • Try `print(element.text)`. – John Gordon Aug 22 '21 at 20:18

2 Answers2

0

Try something like this :

details = driver.find_elements_by_xpath("//div[@id = 'viewcontact']//tr")
for detail in details:
    name = detail.find_element_by_tag_name("h1").text #Or `.get_attribute("innerText")`
    cpny = detail.find_element_by_tag_name("p").text
    print("{} : {}".format(name,cpny))
pmadhu
  • 3,373
  • 2
  • 11
  • 23
0

to get the company name :

wait = WebDriverWait(driver, 50)
print(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.formsection_light h1"))).text)

to get the first person name :

print(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.formsection_light p.f16"))).text)

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38