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.
Related
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
I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.
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
I am trying to get the value from webpage using selenium base on python. However, I do not get the point about how to make it. The value I want to catch is in the red mark side of below picture. Please help me to figure out this. Thank you very much!
Please click here to check the picture
There are few ways (By css,by xpath...) I recommend you reading this page: http://selenium-python.readthedocs.io/locating-elements.html
You also need some understanding about html, css and the DOM but the simplest way to get the value you want is by xpath (But its the slowest)
Open your web browser lets say chrome, right click on the element you want and inspect. It will open developer tools and you will have the html line selected already, right click it again-> copy -> copy xpath
from selenium import webdriver
driver = webdriver.Chrome('/path/to/chromedriver')
driver.get("http://www.web.com")
driver.find_element_by_xpath(the_xpath_you_copied_before_from_dev_tools).text
I'm attempting to make a DeviantART Llamabot for a friend as my first Selenium project with Python 3. I have the bot 99% working except for being able to find the "give llama" button.
The problem seems to be that the menu the button appears under is a popup and I can't just right click and select "copy css selector" in Firefox. As soon as I inspect the element for the give menu the menu closes and the html changes.
I've managed to take a screen shot of a random sample page of the code so I can even see what's there. I've tried learning how CSS selectors work from scratch myself and I've managed to find every nested element EXCEPT the actual items in the list. I've tried looking for Nth child and using the ">" operator. I've attempted searching by class name, name, Xpath, link name, partial link name, nothing has worked.
I've read about this problem inspecting popup elements elsewhere and the suggestions are effectively to write an HTML parser or something to copy the entire html code as it changes and then select it from your copy. I'm not going to do that for this project. It's entirely too much work unless I absolutely have to for some reason.
Honestly at this point I don't even care anymore and I just want someone to outright just tell me what to type in so I can finish this project. This is the screenshot I managed to get. I'm looking for the item highlighted in blue.
Since my code was requested for clarification here it is
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains
binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)
Deviant = browser.get("http://www.deviantart.com/random/deviant")
GiveMenu = browser.find_element_by_css_selector(".i47")
GiveMenu.click()
#GiveLlama = browser.find_element_by_css_selector("")`
Everything except the last line works which is why the last line is commented out until I can figure out what to put in there. No matter what I've tried, including the examples provided by the people answering this question so far, I either get a no such element error or an illegal syntax error.
You can use the xpath selector, in chrome you can use rigth click and click on copy > copy Xpath, or the element selected on your inspector is "div.popup2 .blockmenu a.f.givellama"
You should click the "Give" button first, when the pop up dialog opens, inspect the "Give a Llama Badge" element and identify the xpath. Here is a screen shot.
screen shot to locate "Give a Llama Badge" button
You can find that, the xpath to locate "Give a Llama Badge" button(Should click the Give button first to locate this element).
//a[#class='f givellama']
The xpath to locate "Give button"
//a[#href='#give-give-give']/span[text()='Give']
As you pointed out, when the popup dialog opens, if you try to test the xpath in the browser console via "Firepath" or others, when you type "Enter" or click the left mouse, the popup windows is closed. But don't worry about this, since you already identified the xpath locator, you can debug it in your scripts.