Selenium unable to locate element even if this element exists - python

I'm trying to click on the See Story button where (you need to be logged in): https://www.instagram.com/stories/inoxtag/
Button HTML (with French text):
<button class="_acan _acap _acau _acav" type="button">Voir story</button>
I'm using Selenium and Python.
My code is:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button").click()
And it leads me to this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button"}
I'm using xpath because the button class seems to be obfuscated, so I prefer to do it by xpath.
I know my xpath is correct because if I do F12 on the webpage, then paste my xpath I have my button selected.
Thank you

So I debugged your problem. When you open (driver.get(...)) the link, have you taken into account all the "Allow essential and optional cookies", and then logging in into your account?
If you have debugged this in incognito mode and kept the window open, incognito would have stored your data and so wouldn't ask you for your details again.
But if you debug the script and just put a break point on the line
driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button").click()
Then you proceed to fill your data until it routes you to the page you showed and then you try to execute the code that clicks the button you wouldn't have a problem.

Related

Click button element in Selenium

HTML code for the button I want to click
Can anyone please help me, I'm trying to tell selenium to click a button on a website I'm trying to automate. Ive attached a picture of the HTMl code that I want the Webdriver to click.
Can anyone tell me the best find element option to use to click the button.
Ive looked into Xpath but can't seem to get it working?
Thanks
<use xlink:href="#icon-menu-more" href="#icon-menu-more"></use>
So I was trying to grab the xPath from this line of code.
the Xpath is /html/body/div/div[1]/header/nav/button[1]/span[1]/svg/use
but then when trying to use driver.find_element_by_xpath() i get an error saying unable to locate element.
You can try
driver.find_element_by_id('icon-menu-more')
this will help you to click the button
Other Options are
driver.find_element_by_name('name of element')
driver.find_element_by_class_name('class name of element')
driver.find_element_by_tag_name(tag name of element)
driver.find_element_by_css_selector('css selector of element')

Python selenium cannot detect current page element (button)

Python Selenium cannot detect button located on current page after new tab is being opened, it still sees old page as like html structure... It doesn't click does not copy or do anything with one opened tab, only the one I first opened when run the program here is the code:
driver = webdriver.Chrome(path)
driver.get("https://temp-mail.org/en")
time.sleep(10)
button = driver.find_element_by_xpath('//*[#id="tm-body"]/div[1]/div/div/div[2]/div[1]/form/div[2]/button')
button.click()
time.sleep(5)
time.sleep(5)
driver.execute_script("window.open('https://facebook.com','_blank')")
time.sleep(10)
register = driver.find_element_by_id("u_0_2_/t").click()
I have tried on different website but still not working, also tried clicking on the element of previous tab website and it works, that's why I think it's because selenium cannot detect new tab as new html code to get elements from It throws this error: raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="u_0_2_/t"]"}
(Session info: chrome=91.0.4472.124)
and what i want to do is that, open the website temp-mail.org - copy the email address and then register on fb with that email address - but it simply does not click on page of facebook.com after new tab is released (where fb is) I would really appreciate any kind of help
I'd change your approach to opening a new tab...
As quoted from this StackOverflow thread
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.window import WindowTypes
driver.switch_to.new_window(WindowTypes.TAB)
driver.get("https://facebook.com")
This snippet might change your luck. I'd also be weary of trying to scrape websites like Facebook, as they have pretty serious measures in place to make your life more difficult with this.
Good luck!

Why won't Selenium in Python click the pop-up privacy button?

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.

How to fix different behaviour for find_element in frame between Selenium IDE and Python Webdriver

I would like to implement the following commands which already work in the Selenium IDE (screenshot) to python (see coding).
https://i.ibb.co/ydMWN1R/selenium.png
driver.get("https://www.luckyemoji.com/logout");
driver.get("https://www.luckyemoji.com/login");
element = driver.find_element_by_name('email')
element.send_keys('#####')
element = driver.find_element_by_name('password')
element.send_keys('#####')
driver.switch_to.frame(0)
driver.find_element_by_xpath("//span/div[3]").click()
Following error occours:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span/div[3]"}
(Session info: chrome=72.0.3626.121)
(Driver info: chromedriver=2.35 (0),platform=Linux 4.14.98-v7+ armv7l)
That means, that you are trying to click an element that isnt existing, if you go to the website, do you know wich button you are trying to click?
If you can show me this button, I could help you.
PS: may add another screenshot
PPS: there is a captcha, so you arent enable to enter this competition with a bot (if you are trying to make like 1000 accounts to win money ;D )
Edit:
To get the working xpath, go on the website, press F12, click on the icon in the top left corner (at the window that popped up), than click on the button(the button that the program should click), then right click the thing that is marked at the right side of your screen, then click copy -> xpath
Hope you can understand that xP

Find CSS selector for popup menu item for Selenium in Python 3

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.

Categories

Resources