0

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?

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting a TimeoutException this time
@AbdullahShahriar : The xpath is fine.I have tested before posted as an answer.Could you just check if there any iframe above your table element?
Sorry, I was scraping the wrong file. The solution you posted works fine! Thanks! :D
1

Correct your XPath with :

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

Comments

1
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)

6 Comments

check if your table in the iframe if so you need to switch to iframe before using above code: Please refer updated section to switch to iframe
There's no iframe above the table element. But thanks for pointing it out. I was scraping the wrong file and so the error was raised. Your solution works completely fine!
I have another question in mind, what if the class name random_class_name was not known, how can I access the element?
you mean same contact number element ? if so then you can directly use //span[contains(text(), 'XXXXX-XXXXXX')]
No, assume 'XXXXX-XXXXXX' is also unknown
|

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.