Selenium button click with Python - python

I'm pretty new to programming and Зython so trying to learn more by projects. Currently, workıng on Selenium and trying to automate my Steam login. The code looks like this. parts about the username and password inputs seem to working. I tried several different find methods for my button click but not worked. Using Chrome as a browser. I need some help on how to make that button click work. Thank you.
def login():
browser.get("URL")
browser.find_element_by_id("input_username").send_keys("username")
browser.find_element_by_id("input_password").send_keys("pasword")
element = browser.find_elements_by_class_name("btnv6_blue_hoverfade btn_medium")
element.click()
login()

find_elements_by_class_name will return a list of elements and handle all elements within that list you need to iterate through your list. Currently, you are trying to handle a single web element so you can use find_element_by_class_name to retrieve your desired element, and then you can perform an action on it.
element = browser.find_element_by_class_name("btnv6_blue_hoverfade btn_medium")

Related

How to use find element function of selenium in case of no attributes for name, id, class etc. in the HTML

I'm trying to make a automated login on a website using Selenium. I did everything except cant get to selenium write the login details in the appropriate boxes. Because the driver.find function cant find the specified element of the page or I just suck at it.
For reference I've attached the images for which I want to use the find element on(highlighted in the images):
Also need help in click function of selenium on the following:
I tried using driver find element function by class, class name but cannot understand which value do I have to insert. I did also use driver find elements but still didn't work either.
Try the below XPath expression for email field:
//input[contains(#type,'email')]
And below one for password:
//input[contains(#type,'password')]
driver.find_element() code would look like below:
driver.find_element(By.XPATH,"//input[contains(#type,'email')]").send_keys("your email address here")
driver.find_element(By.XPATH,"//input[contains(#type,'password')]").send_keys("your password here")

Finding XPath without using inspecting element?

I am making an automation for download data from a weather institution.
The issue is that in my effort to make it more independent I am trying to make Selenium to Tab keys to a certain spot, so the Browser focus can "Walk" to the download button. When I call the click() function it doesn't do anything. So I tried to Extract the XPath with the function get_attribute("xpath") but it returns None. How I can extract the XPath?
I am going to paste the issue down here:
Bandera=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input')
Bandera.click()
Bandera.click()
## So Here i just select and dis-select a checkbox just to be near the Download button.
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * 1 )
actions.perform()
#Here i just tabed to the button
Accion=driver.switch_to.active_element
#Maybe, here is when i lost the focus of the button?
Descarga_Actual=Accion.get_attribute("xpath")
Thank you and sorry to borrow your time.
To make a click on hover I would use the following sequence:
your_dropdown_locator.click()
dropdown_option = driver.find_element_by_xpath("dropdown option locator")
actions = ActionChains(driver)
actions.move_to_element(dropdown_option)
actions.click().perform()
But, this is the approach that is usually used for dropdowns.
You use: driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input') This is the main problem of your code.
If you pasted html code of this dropdown (or button you need to click), people would help you to find a better locator. XPath/CSS must be unique. In your case the locator is very bad.
Also, I see no sense making Bandera.click() two times.
In your case, as I understand, you just need to click the button. So the locator is your main problem.
You need to find the correct locator, to wait till the button is clickable and then to click it.
Another problem in what you are trying to do:
get_attribute("xpath") looks like incorrect expectation of how get_attribute function works. Check at least here what this function means Python Selenium: Find object attributes using xpath

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.

Filling out WebForm without "finding element by..." Python Selenium

I am very new to selenium webdriver and I'm using python. I want to go to a webpage that contains(textbox, dropdowns, radio buttons, etc) and I want to tab/move from element to element w/o a "driver.find" syntax because that would defeat the purpose of automating, for some of these pages have a lot of fields. Is there a way to tab from one element to another, without getting the element's id?
Essentially, I would tab from field to field and create an if statement: If this is 'input' then do this, else if 'select' do this, etc.
Any help would be greatly appreciated? I hope I was clear? Thanks in advance.
Yeah you can do this! I will use an example for something with checkboxes:
checkboxes = driver.find_elements_by_xpath('//input[#type="checkbox"]')
for allChecks in checkboxes:
try:
allChecks.click()
except:
print("Checkbox not working")
So this finds every element that is a checkbox and checks it. with allChecks.click(). Notice a couple of things though as when I was learning they were really subtle and I missed.
When finding one element on a page you want to use find_element_by_xpath to find multiple elements on a page use find_elements_by_xpath notice that elements is plural in the second one.

Hidden HTML Elements in Selenium's web driver(python)

I'm writing test scripts for a web page in python using Selenium's remote control interface.
I'm writing it like this:
elem = browser.find_element_by_link_text("foo")
elem.click()
elem = browser.find_element_by_name("goo")
elem.send_keys("asdf")
elem = browser.find_element_by_link_text("foo2")
elem.click()
It then needs to select an item in a list. The list becomes visible when the mouse hovers over it, but selenium cannot find the element if it's hidden. The list also shows options based on who is logged in. The list is implemented in CSS, so trying to run it in javascript and using gettext() does not work.
I've tried searching for the link based on name, class and xpath, but it always reports that it is not visible I've verified from browser.page_source() that the link is in the source code, so it's reading the correct page.
How do I select the link inside the list? Any help is appreciated.
Selenium and :hover css suggests that this can't be done using the Selenium RC interface, but instead must be done using the WebDriver API
Try move_to_element(). Check out the API http://readthedocs.org/docs/selenium-python/en/latest/api.html

Categories