0

I'm trying to extract information with help of selenium and python from this container "PROJECT INFORMATION" //www.rera.mp.gov.in/view_project_details.php?id=aDRYYk82L2hhV0R0WHFDSnJRK3FYZz09 but while do this I was getting this error

Unable to locate element: {"method":"xpath","selector":"/html/body/div/article/div2/div/div2/div2/div2"}

after studying about it I found that this highlighted div is missing and there are many places in this container where div is missing. How am I supposed to do that? I want information only from the right side of the table enter image description here

MY CODE:

for c in [c for c in range(1, 13) if (c == True)]:            

    row = driver.find_element_by_xpath("/html/body/div/article/div[2]/div/div[2]/div["+ str(c) +"]/div[2]").text

    print(row, end="   ")
    print("     ")
else:
    print('NoN')

error:

no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/article/div[2]/div/div[2]/div[2]/div[2]"}
  (Session info: chrome=83.0.4103.106)

1 Answer 1

1

The fields highlighted are two different cases. While for "Registration Number" the required div does not exist, for "Proposed End Date" it exists but contains only white space.

Give this a try instead of the for c... loop. It should handle both cases.

#find parent element
proj_info=driver.find_element_by_xpath("//div[@class='col-md-12 box']")

#find all rows in parent element
proj_info_rows = proj_info.find_elements_by_class_name('row')

for row in proj_info_rows:
    try:
        if row.find_element_by_class_name('col-md-8').text.strip() == "":
            print(f"{row.find_element_by_class_name('col-md-4').text} contains only whitespace {row.find_element_by_class_name('col-md-8').text}")
            print('NaN')
        else:
            print(row.find_element_by_class_name('col-md-8').text)
    except SE.NoSuchElementException:
        print('NaN')

You need this import:

from selenium.common import exceptions as SE
Sign up to request clarification or add additional context in comments.

1 Comment

Forgot the add the import.

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.