Selenium find a element with no class, div, text (Python) - python

I want to make a automatic google login with selenium but i cannot find the elements, the buttoenter image description heren "Next", because the class
is modified each time when we come to start a browser with selenium, or when we reset the login page and does not have Id, but the button is in a div that includes just this button,
I would like someone to help me find a solution to find how I can use this button in order to click it to skip the page where you have to put your email address to skip to the password
i would like to use css selector
(Google Chrome the browser I use)
I code with Selenium 4.2.0 on Linux Unbuntu

driver.find_element(By.CSS_SELECTOR, 'button[class="VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-dgl2Hf nCP5yc AjY5Oe DuMIQc LQeN7 qIypjc TrZEUc lw1w4b"]').click()
That will click the 'Next' button on Google login. Good luck getting any further though. Google seems to block logging in on Chromium.

You can either use jsname as an alternative for finding the element
I found a similar question here How can I inspect element in to div with jsname?
I think the person is doing pretty same thing
Hope this helps :)

Related

Is there any way to click on "plain text" using selenium?

Apologies if this question was answered before, I want to click on an area in a browser with plain text using Selenium Webdriver in python
The code I'm using is:
element_plainText = driver.find_elements(By.XPATH, '//*[contains(#class, "WgFkxc")]')
element_plainText.click()
However this is returning "ElementNotInteractableException". Can anyone help me out with this?
Selenium is trying to be helpful here, by telling you why it won't click on the element; ElementNotInteractableException means it thinks that what you're trying to click on isn't clickable.
This usually happens because either:
The element isn't actually visible, or is disabled
Another element is "overlapping" the element, possibly invisibly
You're clicking something Selenium thinks won't do anything, like plain text
There's two things I'd try to get around this. Firstly, Actions. Selenium has an Action API you can use to cause specific UI events to occur. I'd suggest finding the co-ordinates of the text, then making Selenium click those co-ordinates instead of telling it to click the element. Read more about that API here.
Secondly, try clicking it with Javascript, using a Javascript Executor. That can often give you the same outcome as using Selenium directly, without it being so "helpful".

How to click buttons on amazon with selenium?

I'm trying to click buttons on amazon with selenium (python) but it won't work. It says the elements don't exist. I tried to google it but only found outdated solutions. I've tried Xpaths,ID,full XPATH on pretty much all input fields with no success. I've used selenium before and it works flawlessly but on amazon in particular it can't find the elements. For example the place order button (Je bestelling plaatsen in my language) can't be found nor clicked.
driver.find_element(By.XPATH, '//*[#id="turbo-checkout-pyo-button"]/span/input')
driver.find_element(By.ID, 'turbo-checkout-pyo-button')
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
How do I know which elements are clickable and direct to the intended page? If there is a way to tell please let me know. Thanks in advance
I'm not a selenium master but you can use the find_element_by_xpath function that returns an element object that can be clicked so:
element = driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
element.click()
The problem was that the button was located in an iframe. Switching to the iframe and then pressing the button does the job. Found out through this question
driver.switch_to.frame("turbo-checkout-iframe")
directButton = driver.find_element(By.CSS_SELECTOR, '#turbo-checkout-pyo-button')
directButton.click()

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.

Find login button id with Selenium in Python

I'm trying to log in Discord using selenium in Python, so that I can later run some searches and return data.
So far this is what I have:
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:\\xyz\\chromedriver_win32\\chromedriver.exe')
browser.get('https://discordapp.com/login')
username = browser.find_element_by_id("register-email")
username.send_keys("xyz#abc.net")
password = browser.find_element_by_id("register-password")
password.send_keys("password123")
The next step would be to submit id and password with the Login button, but I do not know how to find that element by name.
browser.find_element_by_name("login").click()
How can I find that element programatically?
It doesn't look like the Login button element has a name attribute equal to 'login', so in this case you wouldn't be able to find it with your browser.find_element_by_name("login").click() statement.
If you're using Google Chrome, you can use the Developer Tools (F12 on Windows, I believe, CMD + Shift + I on Mac) to find the element within the DOM tree. If it had a name attribute, you'd be able to locate that here. In this case it doesn't, but we're able to right click on the element within the DOM tree and can extract another way of finding the element, such as 'Copy XPath' or 'Copy selector'.
Cleaning up the extracted XPath, you could use the following to click the element:
browser.find_element_by_xpath("//*button[contains(., 'Login')").click()
If you open the developers tools using F12 you can put the mouse cruiser on elements in the html and they will be highlighted in the UI.
The login button has class btn-primary
browser.find_element_by_class_name("btn-primary").click()

Python Webdriver need code to find a particular element

I have used Python 2.7, Webdriver and Chrome to access Pinterest to insert images to a board. I have successfully logged in to the site, created a board and clicked on the Pin Image button (thanks to Stack Overflow). The problem that I have is to identify and click the “No Thanks” button using xpath find elements code. I attach an image of the web page and the Chrome inspect on the element.
Pinterest 'Popup'
Not Now Element code
I guess you can give a try to this xpath, which will grap the first element containing the class "cancelButton". Hopefully, the button on your popup will be the first element on the page containing this class.
button[contains(#class, 'cancelButton')]
hope this helps
//span[contains(text(),'Not now')]
this is general syntax: //tag[contains(attribute,‘value’)]

Categories

Resources