Locating Table Element by xPath in Python Selenium - python

I copied the xPath of a certain element I am trying to click and obtained the following:
//*[#id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a
My code then contains the following:
driver.find_element_by_id("//*[#id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a").click()
Any idea why I might be getting this error: r
aise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

XPATHs may contain id (or many other HTML tags), but that does not mean that the id is the XPATH. Use:
driver.find_element_by_xpath('//*[#id="ctl00_ctl00_ctl00_body_homebody_PageMainContent_ResultsGrid_ctl00__0"]/td[1]/a').click()
and it should work - assuming there is a valid XPATH at that location on your website.
also, re: your comment on the OP - //* specifies relative XPATH vs absolute XPATH - essentially meaning that it skips the initial and tags and goes straight to the middle of the DOM.

Related

Get Xpath from already known element

Friends, I'm doing a web scraping. I'm looking for an element that has the XPath changed every time it's searched. For that I'm looking for a way to get the correct xpath.
For this I will need to get the Xpath of an element that I can already locate, it is not the element that I need to locate. But with his Xpath I can find the Xpath of the desired element. so i'm searching:
element = self.chrome.find_element_by_xpath("//div[text()='Apelação']")
With that I need to get your XPath which is:
//*[#id="consultarProcessoForm:dtProcessos_data"]/tr[2]/td[4]/div
How can I do this?
I tried using this; but it returned nothing
print(element.get_attribute("id"))
This is the code of the page:
enter image description here

unable to select an element using xpath in selenium and python

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

How to find the xpath in selenium

I need to find the xpath for the line highlighted in the image; the page is dynamic.
I tried this:
//td[contains(text(),'Ricardo')]
But gave me an error
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element:
{"method":"xpath","selector":"//td[contains(text(),'Ricardo')]"}
You may try:
driver.findElement(By.xpath("//*[text()='Ricardo']"))
I assume you know how to inspect the elements on a page. In the console window, you can then just right click the element you want and copy the Xpath. Once you've copied it, paste in following code: driver.find_element_by_xpath("paste here"). You might see that within the xpath there are double brackets and you will need to replace these by single brackets.

Python selenium: I need help finding elements within elements on Autotask.net

I am trying to find an element on the Autotask webpage in order to interact with it:
I'm using the following code to search within elements:
header=driver.find_element_by_css_selector('html')
body=header.find_element_by_css_selector('body')
body1=body.find_element_by_css_selector('#WorkspaceContainer')
body1.find_element_by_css_selector('#WorkspaceContainer > div:nth-child(1)')
body2=body1.find_element_by_css_selector('#WorkspaceContainer > div:nth-child(1)')
body2.find_element_by_css_selector('#PageContainer')
body3=body2.find_element_by_css_selector('#PageContainer')
body3.find_element_by_css_selector('#PageContainerFrame')
body4=body3.find_element_by_css_selector('#PageContainerFrame')
body4.find_element_by_css_selector('html')
body5=body4.find_element_by_css_selector('html')
body5.find_element_by_css_selector('body')
body6=body5.find_element_by_css_selector('body')
It seemed like I was successfully navigating through them, but it fails at this line with the exception:
body4.find_element_by_css_selector('html')
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"html"}
I was using mouse movements and clicks via pyautogui module to interact with the site, but I've been told that interacting with the element itself is more reliable. Can anyone assist?
Seems that you're trying to handle element inside an iframe.
To do this you need to switch to this frame at first:
body3.switch_to_frame('PageContainerFrame')
body3.find_element_by_css_selector('html')
P.S. If you need to handle just one element, you'd better to point on it directly with relative XPath instead of consecutive stepping from parent elements to child...

Python selenium test - Facebook code generator XPATH

I'm trying to get the XPATH for Code Generator field form (Facebook) in order to fill it (of course before I need to put a code with "numbers").
In Chrome console when I get the XPATH I get:
//*[#id="approvals_code"]
And then in my test I put:
elem = driver.find_element_by_xpath("//*[#id='approvals_code']")
if elem: elem.send_keys("numbers")
elem.send_keys(Keys.ENTER)
With those I get:
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
What means wrong field name. Does anyone know how to properly get a XPATH?
This error usually comes if element is not present in the DOM.
Or may be element is in iframe.

Categories