Python selenium test - Facebook code generator XPATH - python

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.

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

Error when switching to iframe with Python Selenium

Currently I'm attemping to switch from my default content, to the only iframe in the website. I don't know if it's how the site is coded, but I can't access via DOM any element.
This is the HTML structure of the site:
XPATH of site is //*[#id="iframeBody"] (When I paste this in the element inspector, I get the correct iframe). So, if I try to switch using frame_to_be_available_and_switch_to_it, this is the output:
try:
WebDriverWait(self.driver, 10).until(ec.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframeBody' and #name='body']")))
except Exception as e:
print(e)
>>> Message: javascript error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
>>>(Session info: chrome=79.0.3945.88)
I also tried creating an iframe element variable, finding it via ID and XPATH and then I've used switch_to(element). Getting the same result. When I print this variable, the element is actually found:
# Also tried finding with id
element = self.driver.find_element_by_xpath('//iframe[#id='iframeBody')
print(element)
<selenium.webdriver.remote.webelement.WebElement (session="9184691b1fdcccc15dd36bbcb914ac8b", element="1ef77729-8a6e-4d3c-98bd-c95878437585")>
But when I try to switch to this iframe, I get the same result as above.
For some reason, this site is not letting me use the DOM data, actually, when I try to click a button I need to use action chains, because I get the same error.
Anybody can help me?
Did you put enough time before switching ?
also, you could directly switch as well:
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("iframeBody"))

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)

Send_keys function triggers error message: 'Message: element not interactable'

I'm using Selenium to fill out this HTML form, but when it comes to inputting the data it says 'element not interactable'. I am able to click on the element however actually sending a string produces an error. How can I fix this?
driver.get('https://www.masmovil.es/cobertura-fibra-optica-campos/')
prov = Select(driver.find_element_by_xpath('//*[#id="province"]'))
prov.select_by_index(32)
driver.find_element_by_xpath('//*[#id="town"]').send_keys('1')
Thank you!
In the page you are accessing there are 2 elements that are returned with the selector by_xpath('//*[#id="town"]'), one is a "mm-ui-autocomplete", the other one is an "input".
the "mm-ui-autocomplete" is not visible nor interactable to a real user, that's probably what's throwing the exception you're having, and selenium always takes the first match when there's more than one element returned by the selector, so, assuming you want to type something on the "Localidad" field, it is selecting the wrong element.
Try changing your selector to by_xpath('//input[#id="town"]') and see if it works.
Hope it helps.
Can you try with this css selector :
input[id='town']
code :
driver.find_element_by_css_selector("input[id='town']").send_keys('1')
The xpath (//*[#id="town"]) you have used has two entries :
one with mm-ui-autocomplete tag and one with input tag.
Always give preference to css selector over xpath. It's more stable then xpath.
In case you would not want to use css selector, then you can use xpath like this :
//input[#id='town']
Code :
driver.find_element_by_xpath("//input[#id='town']").send_keys('1')
In my case, it happens that the find_element was not working before the frontend finished loading.
I solved this by adding sleep(2) before the find_element_by_xpath. You will need to import the function by from time import sleep.

Click on li element searching specific displayed text

Using python 3 and chrome driver. I'm trying to click on my desired element searching for the text displayed on this page . For example, in case of "BEBES" I'm using:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(text(), "BEBES")]'))).click()
but nothing happens. Just throws the time out exception. What's my error?
Your xPath is not correct. Use this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[contains(text(), "Bebes")]'))).click()
Note: upper/lowercase makes difference
and
This post suggests using the following as text() returns a node set:
//*[text()[contains(.,'BEBES')]]
XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Categories