1

I've been trying to select an element by xpath and display it but I get an error everytime I try to run the code. I got the xpath by doing inspect element and copying full xpath yet it gives me the error. It's a dynamic form too, so I can't choose the direct text and I would probably need to use an address to locate that element as it changes everytime but I've not been able to select that certain element. how do I choose the element?

this is how I tried to choose the element

name_from_doc=browser.find_element_by_xpath('/html/body/form/div[3]/div[3]/div/div[4]/div/div[2]/div[4]/div[2]/text()[1]')
print(name_from_doc)

the error that I get is

InvalidSelectorException: Message: invalid selector: The result of the xpath expression "/html/body/form/div[3]/div[3]/div/div[4]/div/div[2]/div[4]/div[2]/text()[1]" is: [object Text]. It should be an element.

I want to store the name of the person separately and address separately in two different variables

enter image description here

2 Answers 2

1

To get the value NAPERVILLE IL Use follwoing xpath to get the element and then use splitlines() and last index value.

name_from_doc=browser.find_element_by_xpath('//div[contains(.,"Billing Address")]/following::div[1]').text
print(name_from_doc.splitlines()[-1])

Update:

name_from_doc=browser.find_element_by_xpath('//div[contains(.,"Billing Address")]/following::div[1]').text
print(name_from_doc.splitlines()[0])
print(name_from_doc.splitlines()[1])
print(name_from_doc.splitlines()[-1])
Sign up to request clarification or add additional context in comments.

7 Comments

I want to get the name of the person too? how do I do that?
and the other address too.. what is splitlines is doing?
@reduxHelpPlz: this will split lines if the text having any lines in your care <br> are nothing but lines.
is there any way through which I could get only the first two numbers instead of the whole address in the second line? i mean now it would print 10 MUIRHEAD but I only needed the 10 and not muirhead
Sorry mate it’s too late here in UK.Shall I comeback tomorrow.See if you can do that take the second print statement in a variable such as ele and then split. print ele.split(“ “)[1]
|
1

As the Billing Address text would always be there, so can reach there by using its text in the xpath and then find its exact value by using following in the xpath.
You can do it like:

name_from_doc = browser.find_element_by_xpath("//div[contains(text(),'Billing Address')]//following::div[1]//br[2]")
print(name_from_doc.text)

3 Comments

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(@text,'Billing Address')]//following::div[1]//br[2]"} got this error
@reduxHelpPlz have edited my answer, had mistake a mistake in the last one. Please pick the latest answer.
didn't get any error but nothing shows in the display!!

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.