Python selenium cannot detect current page element (button) - python

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!

Related

Python Selenium how to interact with a specific button on a web page

Hi i'm trying to interact with this web element in the following link https://www.gurufocus.com/stocks
Here you can find the image
I want to set it to 100 so that i can visualize 100 stocks at the time i've tried to interact with it by using the following code
wd.find_element_by_xpath("/html/body/div[1]/div/div/div[1]/section[2]/main/div/div[1]/div[3]/div[1]/span/div[2]").click()
wd.find_element_by_xpath("/html/body/div[2]/div[6]").click()
But only the first line works the second line gives me the following error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='el-popover-2775']"}
(Session info: chrome=106.0.5249.119)
It is not able to locate it i have also tried with the method find element_by_id but still not able to locate the button that sets the number on 100 This one does anyone has an other solution? Thanks in advance let me know if you need more information i will answer as soon as possible.
you can select the dropdown like this:
from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME, "aio-tabs-button").click()
and select 100 like this:
driver.find_element(By.XPATH, "//div[#class='item' and contains(text(), '100')]").click()
However, I'd recommend using the API and a package like requests instead of simulating the browser.

Selenium unable to locate element even if this element exists

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.

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.

getting error during selenium automation selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: python

i need to click second row submit buttonI am trying to use the XPath of the tab present on the website and collected XPath using chrome/firefox but it seems that every time I am getting an exception as below:
I have kept sleep time and can see website loading pretty early than the sleep time still getting the errors.
said_input = driver.find_element_by_xpath('//*[#id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder3Col_txtSubmitCase"]')
print(said_input)
said_input.send_keys(said)
exception:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder3Col_txtSubmitCase"]
One thing is that i have navigated to another page of website by clicking one of the menu button will this driver.find_element_by_xpath will be able to locate it. if not how can i do so in case if i hae to browse through multiple pages and fill few forms.

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

Categories

Resources