How to find the xpath in selenium - python

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.

Related

Selenium unable to find Button

I am trying to click on the "Next Page"-Button on the Web of Science Search-Site to iterate through all pages.
Here is a screenshot of the HTML of the page (highlighted is the button)
This is my code to find the button:
driver.find_element_by_class_name('mat-focus-indicator mat-icon-button mat-button-base').click()
But I receive this error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".mat-focus-indicator mat-icon-button mat-button-base"}
I have tried so many ways of identifying the button (find_by_id, find_by_name, find_by_link_text) but nothing works.
What am I doing wrong?
Thank you in advance
Maybe try with Query Selector like:
driver.find_element_by_css_selector('button[data-ta="next-page-button"]')
You can always try selectors on elemnts panel (like on screenshort) and type your selector in field "Find by string, css, or xpath"
Class name do not have support for spaces, remove spaces and put . to make it css selector :
driver.find_element_by_css_selector('button.mat-focus-indicator.mat-icon-button.mat-button-base').click()
also try to put some wait before this to make it more consistent.

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)

Locating Table Element by xPath in Python Selenium

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.

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 cannot send string to website

I need to type some text in online notepad but cannot find the right element to send it.You can find whole page here
I try: fox.find_element_by_xpath(".//*[#id='tinymce']/p")
but error occured:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: {"method":"xpath","selector":".//*[#id='tinymce']/p"}
Also I try:
fox.find_element_by_xpath("//div[contains(.,'Working')]") no errors but it didnt type the string
Since the 'notepad' is in an iframe, you first need to switch to the specific frame. Try this:
fox.switch_to.frame(fox.find_element_by_id("page_text_ifr"))
notepad = fox.find_element_by_id("tinymce")
notepad.send_keys("Hello World!")
Or since you use XPath:
fox.switch_to.frame(fox.get_element_by_xpath("//iframe[#id='page_text_ifr']")
notepad = fox.get_element_by_xpath("//body[#id='tinymce']")
notepad.send_keys("Hello World!")

Categories