Python selenium - cant find element - python

I am not completely new to selenium but I cannot work this out. I am supposed to do a basket automation and I have troubles at the first page.
I am supposed to fill three input boxes at first to proceed to the actual order but I fail at the first input. We have unique ids for almost every input so it should not be too hard to find it but somehow it is.
<input class="form-input--text" data-validitytext-empty="Položka je povinná.<br>Prosíme, vyplňte ji." data-validitytext-invalid="Položka má špatný formát.<br>Prosíme, opravte ji." data-label-selector="closest(.form-item--required)->find(label.form-label)" id="surname003" name="Prijmeni" type="text" value="" data-form-required="true" xpath="1">
This is how the first input is described with id of "surname003" so my first try was to do this
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("text")
Then I tried to access it with id
driver.find_element_by_id("surname003").send_keys("text")
Nothing from this was working so I tried to get the full xpath of it but that did not work.
This is how the code looks as a whole
driver = webdriver.Chrome(executable_path=r"C:\Users\KDK\Desktop\Selenium setup\chromedriver.exe")
driver.get(URL2)
driver.maximize_window()
driver.implicitly_wait(10)
driver.find_element_by_xpath("//*[#id='surname003']").send_keys("test")
driver.find_element_by_xpath("//*[#id='phone03']").send_keys(telefon)
driver.find_element_by_xpath("//*[#id='email03']").send_keys(email)
This is the error I get
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
This is the page I am working with :
https://www.nev-dama.cz/zima/rakousko/lungau/residence-carpe-solem-mariapfarr/rezervaceNova?termin=2021-01-10&typologie=[26225]&delka=3
I am thinking if the issue of locating the element is somehow connected to the fact that you can switch between two "windows" in the reservation page. You can switch between "Not binding reservation" and "Buy online" and I dont know if that makes it somehow harder to get to the element.
I hope someone can help me with this. If something is not clear please ask me and thanks for reading and thanks in advance for some answers! :)

They have few inputs with the same id and first is from another from. But it mustn't raise NoSuchElementException.
Try XPATH like that
//div[#id="koupit-online"]//*[#id='surname003'].
You need to add this part //div[#id="koupit-online"] to all all xpath or use as context
form = driver.find_element_by_xpath("//div[#id='koupit-online']")
form.find_element_by_xpath(".//input[#id='surname003']").send_keys("test")
form.find_element_by_xpath(".//input[#id='phone03']").send_keys(telefon)
form.find_element_by_xpath(".//input[#id='email03']").send_keys(email)
Second problem can be chromedriver. When i used chrome with selenium i got this exception without reason. I changed browser to firefox and geckodriver and this never happend again

Related

I have a selenium python issue on select an html element icon and click

I'm trying to click to close a message from a website with selenium. However, when I put it to click, a message appears in the Visual Studio Code console saying that it was not possible to click on the element because it is not a clickable element.
sleep(5)
web.find_element(By.XPATH, '//*[#id="top-container"]/div[1]/div/i').click()
devtool element
error https://i.stack.imgur.com/sgzoE.png
if anyone knows any library that delete the element in devtool. why do i need to remove that message to appear another button to proceed with application
If you look at the error message carefully, it doesn't state that it's not a clickable element. It states that the click was intercepted. In other words, Selenium tried to click on the X to close but another element, an <h3>, got in the way.
It looks like your locator is fine. According to the error message, it looks like it's finding the right element. I personally would change it to
web.find_element(By.CSS_SELECTOR, 'i.icon-remove').click()
because I think it's more readable and less likely to click the wrong element.
I can't see the page so I have no idea what h3 is getting in the way and if it's possible to even remove it. So, if you can't get around the h3, you will likely have to use JS to click the element.
icon = web.find_element(By.CSS_SELECTOR, 'i.icon-remove')
driver.execute_script("arguments[0].click();", icon)
There are different ways to call an element. Let's try the following:
web.find_element(By.ID, "top-container").click()
Please let me know if works, either way we can see other options

selenium cant find element - no such element

I have this website where I want to get the element post-title.
I tried driver.find_element_by_class_name("sc-1di2uql-1 vYcWR") but that just returns: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".sc-1di2uql-1 vYcWR"}
I have seen similar posts where the problem was that the searched element was in an iframe but I cant see anything like that here. Then again I started to learn about webscraping about a week ago so theres probably a lot I dont know.
I had a similar issue with selenium before. My problem was that the website would rotate class names, so every time you access the page they would be different. Judging by the way the class names are on your website I think you have the same issue. If this is the case, you are just trying to look for something that is not there. What I did to solve this problem was using driver.get_element_by_xpath(“PASTE FULL XPATH OF THE ELEMENT”) instead of finding the element using its css class.
To get the full path of an element: in the developer tools, right click on the element and click copy FULL XPATH.

Selenium Python Help Getting element of a form

Iv been trying to get the form elements on this website and for some reason I cant get them. Iv tried every way I can think of. I would really appreciate some help.
Here is my latest attempt
inputs = driver.find_elements_by_xpath("//input[#type='text']")
Here is the website.
Thanks in advance.
Edit : Github link for project
Edit : Ok so I found out that if you go straigt to website that the btn points to then it works. So I am assuming that slenium does not automaticly update to the HTML of the current page. How does one do this?
if you are getting 'NoSuchElementException', try using conditional wait before you do find_element. When you are loading another page wait until the element is visible and can be interacted.
Guide for that is here
check if the element is inside in iform, if so use
driver.switchTo().frame("framename")
EDIT
Focus on something unique in the tag. For your example use the class name used in the div. Do not use the ID tag identifier as it changes over time.
//*[contains(#class, 'ginput_container ginput_container_text')]/following::*[contains(text(),'Email')]/following::div[1]/descendant::input

Can't find element by id using selenium

I am making a script in python that goes on a webpage https://www.realtor.ca/ and searches for a specific location. My problem is at the very beginning. When you open a page in the middle is a large search element. Html for that element is:
<input name="ctl00$elContent$ctl00$home_search_input"
maxlength="255" id="home_search_input" class="m_hme_srch_ipt_txtbox"
type="text" style="display: none;">
I am trying to access the element with find_element_by_id method but I always get the error:Message: Unable to locate element: [id="home_search_input"]
This is my code:
from selenium import webdriver as web
Url = "https://www.realtor.ca/"
browser = web.Firefox()
browser.get(Url)
TextField = browser.find_element_by_id("home_search_input")
Has anyone encountered a similar problem or does anyone know how to fix it?
When navigating to the page, the element with the id home_search_input isn't visibble at first. It seems that this one only gets visible once you click the "Where are you looking" placeholder (which disappears then). You'll need to do this explicitly in your test.
Additionally make sure to use either implicit or explicit wait statements to ensure that the elements you interact with are properly loaded and rendered.
Here's an example for your page using the Java client bindings - Python should be quite similar:
driver.get("https://www.realtor.ca/");
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("m_hme_wherelooking_lnk"))).click();
driver.findElement(By.id("home_search_input")).sendKeys("demo");

Trying to find and click link in Selenium with Python

I have a page that contains the following line of html
<td><span><a onClick="setSummaryClasses(3)">Reality</a></span></td>
Copied from the FF Source panel. I try to click the anchor with
driver.find_element_by_link_text('Reality').click()
and I get a
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"Reality"}'
What am I missing?
I have both
driver.set_script_timeout(10)
driver.implicitly_wait(10) # seconds
active.
Ensure the link isn't inside any kind of frames. Otherwise please use switch_to_frame first.
When you match link text, bear in mind it does exact match, so if you have messy whitespaces, please try find_element_by_partial_link_text
driver.find_element_by_partial_link_text('Reality').click()
Changing locators might also be helpful.
driver.find_element_by_css_selector("a[onClick='setSummaryClasses(3)']").click()
Thanks to user1177636, I realised that, when a click opens a window, in the LIVE situation the new window becomes the current "top" window, while in Selenium it doesn't.
Therefore I needed to
driver.switch_to_window('Chat23')
before looking for the element.
Hope this helps somebody.

Categories

Resources