简体   繁体   中英

Extract text from span element using selenium in python

I'm trying to extract the contact number (XXXXX-XXXXXX) from the following HTML code using Selenium webdriver's find_element_by_xpath method.

 <div id="contact-info">
                    <div class="a">
                        <div class="b">
                            <table class="c">
                                <tr>
                                    <td class="d">
                                        <div class="e">Contact Info</span></div>
                                    </td>
                                    <td class="f">
                                        <div></div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="g">
                            <div class="h">
                                <table>
                                    <tr>
                                        <td>
                                            <div class="i"><span class="j">Mobile</span></div>
                                        </td>
                                        <td class="k">
                                            <div class="random_class_name"><span><span dir="ltr">XXXXX-XXXXXX</span></span>
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                            </div>

This is my code for extracting the required data-

print(browser.find_element_by_xpath('//div[@id="contact-info"]/div[1]/div[2]/div[1]/div[2]/span[1]/span[1]').text)

But I'm thrown with an exception saying the element can't be located. What am I possibly doing wrong here? How can I fix this?

Correct your XPath with:

print(browser.find_element_by_xpath('//span[@dir="ltr"]').text)

To get the Mobile-Number you can take the reference of the text Mobile and find the next sibling.

Induce WebDriverWait () and wait for visibility_of_element_located () and following xpath.

print(WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//td[.//div[contains(.,'Mobile')]]/following-sibling::td[1]/div"))).text)

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
contactNumber = WebDriverWait(browser, 30).until(
            EC.element_to_be_clickable((By.XPATH, "//div[@class='random_class_name']//span[1]//span")))
print contactNumber.text

or

contactNumber = WebDriverWait(browser, 30).until(
    EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'XXXXX-XXXXXX')]")))
print contactNumber.text

Note : add below imports to your solution:

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

Upddated Section: Check your table is in the iframe if so then use below code to swiutch to iframe;

iframe=driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM