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

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.

Related

Clicking multiple <span> elements with Selenium Python

I'm new to using Selenium, and I am having trouble figuring out how to click through all iterations of a specific element. To clarify, I can't even get it to click through one as it's a dropdown but is defined as an element.
I am trying to scrape fanduel; when clicking on a specific game you are presented with a bunch of main title bets and in order to get the information I need to click the dropdowns to get to that information. There is also another drop down that states, "See More" which is a similar problem, but assuming this gets fixed I'm assuming I will be able to figure that out.
So far, I have tried to use:
find_element_by_class_name()
find_element_by_css_selector()
I have also used them in the sense of elements, and tried to loop through and click on each index of the list, but that did not work.
If there are any ideas, they would be much appreciated.
FYI: I am using beautiful soup to scrape the website for the information, I figured Selenium would be helpful making the information that isn't currently accessible, accessible.
This image shows the dropdowns that I am trying to access, in this case the dropdown 'Win Margin'. The HTML code is shown to the left of it.
This also shows that there are multiple dropdowns, varying in amount based off the game.
You can also try using action chains from selenium
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
Source: here

Selenium not detecting an element

So I'm coding this bot to join my school classes for me using selenium and I'm facing this issue where it goes to my conferences page and it waits for a join button to appear and press on it. At first, it wouldn't be able to find it so I scratched the idea of time and I made sure that when it opens the page the join button will be there so I did that and even put this line of code just to make sure
join=WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Join')]")))
yet it still can't find the join button either press it even though it's visible by the eye I will link the page source under in a link if you can just tell me how to find it also I cant use the href method since it isn't stored like that it's just stored like this
https://i.stack.imgur.com/ldxY7.png
so I'm really confused it's almost like this is an invisible element its funny because even when I do get an error I use control F on inspect element to check if I used correct XPATH and I always find it so I don't see why selenium cant please help I think I just missed something I just don't know what.
Page Source
Check the presence of the element by looping and checking the presence_of_element_element_located multiple times with 2/3 seconds delay added between every consecutive check.
An analogy from python selenium-
for i in range(5):
l= driver.find_element_by_css_selector("#js-gdpr")
time.sleep(5)
OR
Iterate over all the elements under the parent div and verify it using unique text/color and access it.
I think you are trying to do Google meet automation. In that website the "join now" button is declared as a dynamic path so, you can use the library pyautogui
impor pyautogui as pg
for i in range(5):
pg.press('tab')
time.sleep(2)
pg.press('enter')
This code will help you to find the join now button and to click it.

How can I use selenium in python to select an item in a dropdown when the options are non-interactable <div> boxes?

I am trying to make a program that runs through the infamous https://userinyerface.com/ using selenium. However, I am getting stuck on the second page where there is a dropdown menu requiring you to select a top level domain. The dropdown menu is entirely made of divs and css, meaning none of the options have unique IDs, and seemingly cannot be interacted with using .click():
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
This is the entire HTML for the dropdown:
HTML code
What can I actually do? I read about the Select class as well, but I assume it will have the same result, and the elements are not uniquely identifiable by ID so not sure it can be used either way.
try this
driver.find_element_by_class_name('dropdown__list_item selected')[option_you_want_to_click_index].click()
This will help. You can have array with all of the selections and just click the first one.
Mas = driver.find_elements_by_class_name("dropdown__list-item")
Mas[1].click()

Trouble clicking button using Selenium with Python

I am trying to make a basic form filler using Selenium on nike.com. I have completed most of it but I am having trouble clicking on the button to select the gender. I have attempted to use many examples of find_element_by_xxxxx code bit none of it has worked. Finding elements by id and xpath haven't come to much either. A typical error i get is Message: no such element: Unable to locate element. I am very new to coding so I could very easily have made an error, but any idea on how you guys would solve it would be much appreciated
That XPATH is very long and you can simplify.
By the looks of it , I would guess those Ids are changing every time there is a new session.
A more straightforward XPATH selector could be...
"//span[text() = 'Male']"
// specifies to search the entire document
span specifies the type of element to search for
text() specifies text that needs to be inside the element
(this will give you the span element but it should still work)
or
"//span[text() = 'Male']/parent::li//input"
(this will give you the actual input button)
Also , like Ollin Boer Bohan suggested, look into using waits before performing actions on your elements.
#cavan answer is correct, Also you can use xpaht like this too
//input[#type='button']//following::span[text()='Male']
Here we can use following to locate the male, same you can do for female button

Python Selenium keeping elements after changed browser

I'm currently using Selenium on Python and have got a question about it.
elements = driver.find_elements_by_css_selector("div.classname a")
for element in elements:
element.click()
driver.back()
Since coming back to the previous page using back() in this code, Selenium couldn't find elements anymore, even though I still need it.
If someone has got any clue, please help me out.
Many appreciate in advance
Selenium creates a whole new set of objects when you change pages -- whether you click a link, or go back a page. If clicking on the element in line 3 causes Selenium to load a new page, you're getting a StaleElementException on the second element test. So what you have to do is every time you execute driver.back(), you need to search for the element objects on the page as you do in the first line, and probably maintain at least a counter as to how far down the list of elements you've already clicked (assuming they navigate away from the page). Make sense?
You can store the elements in a list and work on them with a loop. For example:
elementList = driver.find_elements_by_css_selector("div.classname a")
for i in range(len(elementList)):
element = driver.find_elements_by_css_selector("div.classname a")[i]
element.click()
driver.back()

Categories