Click checkbox with python selenium - python

so I wanted to click a checkbox on website using selenium (python).That's the button I want to click
So I thought that it would work with that code:
driver.find_element_by_xpath("//input[#name='termsCheck']").click()
But that gives me an errorThat's the error I get
Additional info: there are 2 more checkboxes on the same page which have also <span class="custom-checkbox"> ::before ::after </span>
Has anyone an idea how to get selenium to click the checkbox?

I have seen some scenarios were the element must be clicked with javascript because it is covered by other elements. Alternatively you could click the <span> element that is covering it.
Here is how to click the element with javascript using python and selenium. Since you have not provided the HTML I am assuming that the xpath you provided uniquely identifies the element you want to click.
element_to_click = driver.find_element_by_xpath("//input[#name='termsCheck']")
driver.execute_script("arguments[0].click();", element_to_click )

On most browsers you should be able to copy the XPath or CSS selector by right clicking the specific element on the developer tools console. The click() method should work.

The code is attempting to click the checkbox and Selenium API doesn't like that. The error informs about that, but is not specific enough. Try using auxiliary class Select instead:
from selenium.webdriver.support.ui import Select
element = driver.find_element_by_xpath("//input[#name='termsCheck']")
select = Select(element)
select.select_by_index(index)
Additionally, make sure that XPath //input[#name='termsCheck'] is only matching single element.
Refer to Selenium Python documentation for more details.

Related

clicking on button no ID in Selenium python

Hello I am trying to click on a button within python selenium that has no ID. The button has the text Submit an application within it. However there are multiple phrases of "Submit an application" on the website. The attributes of the website are:
<a class= "button" href = "(a link)" analytics-on="click" analytics-category="Body" analytics-event="(a link)" analytics-label="Submit an application||(a link)">
According to the documentation, there are multiple methods you can use to locate an element.
In this situation, I would personally use Xpath if available. Open the chrome developer tool on the HTML tab and find the element you want to locate (or right click on the element and go to inspect element). Right click on the HTML tag (in the dev tool) and select copy > xpath.
Now in your python code you can locate your element like so:
button = driver.find_element_by_xpath("past_the_xpath_here")
Alternatively because you are trying to locate a link, you could also locate the element using the link text:
button = driver.find_element_by_link_text("Submit an application")
you can try with css_selector.
driver.find_element_by_css_selector('[analytics-on="click"]').click()
driver.find_element_by_css_selector('[analytics-category="Body"]').click()
or link_text method is also a good choice in locating an a tag,
driver.find_element_by_link_text("Submit an application").click()

Python Edge Driver Web Automation Help - Cannot find Xpath

Inspect Youtube Page Element
I am new to Python and I am learning how to automate webpages. I under the basics around using the different locators under the inspect element tab to drive my code.
I have written some basic code to skip youtube ads however I am stuck on finding the correct page element to agree to the privacy policy pop up box in Youtube. I have used ChroPath to try and find the xpath of the page however there doesn't appear to be one. I was unable to locate any other page elements and I was wondering if anyone has any ideas on how I can automate the click of the 'I Agree' button?
Python Code:
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
driver = Edge(options=options)
driver.get('http://www.youtube.com')
def agree():
while True:
try:
driver.find_element_by_xpath('/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-upsell-dialog-renderer/div/div[3]/div[1]/yt-button-renderer/a/paper-button').click()
driver.find_elements_by_xpath('.<span class="RveJvd snByac">I agree</span>').click()
except:
continue
if __name__ == '__main__':
agree()
Youtube Inspect Element Screeshot is below:
I don't know if the xpath in your code is right as I can't see the whole html structure of the page. But you can use F12 dev tools in Edge to find the xpath and to check if the xpath you find is right:
Open the page you want to automate and open F12 dev tools in Edge.
Use Ctrl+Shift+C and click the element you want to locate and find the html code of the element.
Right click the html code and select Copy -> Copy XPath.
Then you can try to use the xpath you copy.
Besides, find_elements_by_xpath(xpath) will return a list with elements if any was found. I think you need to specify which one element of the list to click with. You need to pass in the value number of the elements list like this [x], for example:
driver.find_elements_by_xpath('.<span class="RveJvd snByac">I agree</span>')[0].click()
When inspecting the page elements I overlooked the element of iframe. After doing some digging I came across the fact I had to tell the Selenium Driver to switch from the main page to the iframe. I added the following code and now the click to the 'I Agree' button is automated:
frame_element = driver.find_element_by_id('iframe')
driver.switch_to.frame(frame_element)
agree2 = driver.find_element_by_xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span").click()
driver.switch_to.default_content()

Using selenium to click search button

I am trying to click the search button in this link here
I would like to click the search button and then download all URL's on the next page but currently it is finding monthly list
The link below takes you to a screenshot of my code where it outputs the monthly list button instead of searcj/
Python code selenium
Welcome to Stack Overflow!
You can use the .click() method to click an element object, and the .find_element_by_xpath() method to find the element. I've located that the element's full XPATH is /html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2].
You can implement all the pieces together like so:
driver.find_element_by_xpath("/html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2]").click()
if you are starting I recommend you using some extension to help you find the xPath of the elements.
I used Xpath Helper from Chrome, but you can use any other.
The Xpath of the Search button is:
/html[#class='js']/body/div[#id='idox']/div[#id='pa']/div[#class='container'][2]/div[#class='content']/div[#class='tabcontainer']/form[#id='weeklyListForm']/fieldset/div[#class='buttons']/input[#class='button primary']
After you have the XPath you can use it in the selenium driver to find the elements.
This can be enough, but I recommend you to learn in depth how this works to know exactly what it is doing.

Why does trying to click with selenium brings up "ElementNotInteractableException"?

I'm trying to click on the webpage "https://2018.navalny.com/hq/arkhangelsk/" from the website's main page. However, I get this error
selenium.common.exceptions.ElementNotInteractableException: Message:
There's nothing after "Message:"
My code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get('https://2018.navalny.com/')
time.sleep(5)
linkElem = browser.find_element_by_xpath("//a[contains(#href,'arkhangelsk')]")
type(linkElem)
linkElem.click()
I think xpath is necessary for me because, ultimately, my goal is to click not on a single link but on 80 links on this webpage. I've already managed to print all the relevant links using this :
driver.find_elements_by_xpath("//a[contains(#href,'hq')]")
However, for starters, I'm trying to make it click at least a single link.
Thanks for your help,
The best way to figure out issues like this, is to look at the page source using developer tools of your preferred browser. For instance, when I go to this page and look at HTML tab of the Firebug, and look for //a[contains(#href,'arkhangelsk')] I see this:
So the link is located within div, which is currently not visible (in fact entire sub-section starting from div with id="hqList" is hidden). Selenium will not allow you to click on invisible elements, although it will allow you to inspect them. Hence getting element works, clicking on it - does not.
What you do with it depends on what your expectations are. In this particular case it looks like you need to click on <label class="branches-map__toggle-label" for="branchesToggle">Список</label> to get that link visible. So add this:
browser.find_element_by_link_text("Список").click();
after that you can click on any links in the list.

How to click this button with python & selenium

I'm looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.
I'm having trouble identifying the button to then call the click function.
Here's the HTML
Alternatively perhaps I could run the 'ng-click' function, unsure how to approach that. If I do this, I'll need to pipe through 'index', from what I can tell from this HTML (my understanding of HTML is minimal)
Thank you
You can have browser to figure out the button CSS selector for you.
Here's how to do that in Chrome:
Open your page in Chrome
Right-click on your button and select the Inspect Element from the context menu
The Inspector window will open with the button's HTML markup selected.
Right-click the selected HTML line(s) and select Copy -> CSS Selector.
Paste the CSS selector into the code below
And here's the code to click your button:
from selenium import webdriver
browser = webdriver.Chrome('/path/to/chromedriver')
browser.get('your/website/url')
button = browser.find_element_by_css_selector('paste the CSS selector here')
button.click()
Hope this helps.
PS: Here's an excellent article (a chapter from the Automate the boring stuff with Python book) on web scraping and browser automation using BeautifulSoap and Selenuim.
Try for xpath as:
//div[#id='channel']//div[#class='channel-list']/div/div/div[#class='ch-btn play']
or
//div[#id='channel']//div[#class='channel-list']//div[#class='ch-btn play']
Let me know if this Answers your Question.
You'll probably want to use CSS Selectors, as they are preferred in selenium over Xpath. Some important notes about html and selenium:
Html is a static language. There is not way to "call" or "run" things in it. That requires use of a different language, like JavaScript.
Selenium mimics an actual user, so selenium is not directly "calling" anything, it is interacting with the page, and the page responds.
python code:
driver = webdriver.Chrome('path/to/chromedriver')
driver.get('your/site/here')
# This is a css selector for the div that you want to click on.
css_selector = "div[ng-click='play($index)']"
# This finds the object that is located at css_selector
button_element = driver.find_element_by_css_selector(css_selector)
# Sends a mouse click to the button_element
button_element.click()

Categories