Python Webdriver need code to find a particular element - python

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’)]

Related

Selenium find a element with no class, div, text (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 :)

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".

Pythno Selenium clicking list item within ui

I am working with python and selenium to click on the Photo/Video button on a facebook page. The HTML associated with this seems to have a list item (li) inside a ui. The html is as in the following image. The button circles is the one I am trying to press.
Can anyone please tell me how should I press the Photo/Video button?
Can you try this code?
I used the xPath method and contains() to compare the text in the div.
By the way, the found object does not have the click related function, and the click function seems to be a tag among its parents
The syntax for finding a parent in xPath is /.. and I used this
https://stackoverflow.com/a/3655588/12582501
driver.find_element_by_xpath('//div[contains(text(),"Photo/Video")]/../../../a').click()
Facebook has an intresting thing: testids
With this IDs you can click all of clickable elements on the site
driver.find_element_by_xpath('//div[#data-testid="photo-video-button"]').click()
In this case you can exec your code, when on page will be another element with text "Photo/Video"

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.

why is selenium webdriver not finding my element

https://www.mykplan.com/participantsecure_net/TermsAndConditions.aspx
I am doing find by id/xpath/name and they all fail for the accept button. Here is my latest effort
driver.find_element_by_xpath('//*[#id="Accept"]').click()
copied straight from chrome web tool
The button is located inside a frame. Given xpath is correct only inside a frame. I tested xpaths in chrome console and this is what I got:
In case of main page (https://www.mykplan.com/participantsecure_net/TermsAndConditions.aspx) xpath couldn't be located:
$x('//*[#id="Accept"]');
[]
In case of frame contents only (https://www.mykplan.com/participantsecure_net/TermsAndConditionsBottom.aspx) xpath could be found:
$x('//*[#id="Accept"]');
[<input type=​"submit" name=​"Accept" value=​"I agree" id=​"Accept">​]
In selenium, I guess you need to switch to a frame before looking for xpath. I think that web driver function
driver.switch_to_frame("frameName")
should help. In your case, frame with buttons is called "bottomFrame".

Categories

Resources