Python Selenium, find a button - python

I have searched stackoverflow and tried everything, but nothing seems to work.
I am using Python3.8 with Selenium 3.141.0.
This is the button:
<button id="task-open-filters-button" class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic" data-original-title="Edit Filter">
<span class="sr-only">Show / hide filter</span>
</button>
What I tried so far:
# Because the page is so slow, I work with try/except to get the element.
# This works fine for a simple link, but not for this button
while True:
try:
# elem = browser.find_element_by_xpath('//button[#id="task-open-filters-button"]')
# elem = browser.find_element_by_xpath('//button[#class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic"]')
# elem = browser.find_element_by_link_text("Show / hide filter")
# elem = browser.find_element_by_xpath("//*[#id='task-open-filters-button']")[0]
# elem = browser.find_element_by_css_selector('.btn.btn-icon.icon-filter.list-filter-button.sn-tooltip-basic')
elem = browser.find_element_by_id("task-open-filters-button")
break
except NoSuchElementException:
print("Waiting for page to load!")
sleep(1)
elem.click()
I do not get another error message, the while loop just does not break.
Do you guys have any idea what else to try?
Thank you for your help!

When I look at your code, I see several potential issues.
There is a missing quote:
browser = webdriver.Chrome(chromedriver.exe")
Also - are you sure chromedriver.exe is the correct path? Maybe better provide the absolute path.
The button is commented out (although # is no html comment).
# <button id="task-open-filters-button"
# class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic"
# data-original-title="Edit Filter">
# <span class="sr-only">Show / hide filter</span></button>
This definitely is correct syntax:
elem = browser.find_element_by_id("task-open-filters-button")
So I suggest make sure your setup is correct. Do something simple. Load an URL and return the text from the page.
P.S.: Have a look at this tutorial
http://jonathansoma.com/lede/foundations-2018/classes/selenium/selenium-windows-install/
It looks like the configuration is more like this
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.nytimes.com")

Related

ElementNotInteractableException: Message: Element <button class="btn btn-outline-light btn-icons" type="button"> could not be scrolled into view

I am trying to press a the download on this page
https://data.unwomen.org/data-portal/sdg?annex=All&finic[]=SUP_1_1_IPL_P&flocat[]=478&flocat[]=174&flocat[]=818&flocat[]=504&flocat[]=729&flocat[]=788&flocat[]=368&flocat[]=400&flocat[]=275&flocat[]=760&fys[]=2015&fyr[]=2030&fca[ALLAGE]=ALLAGE&fca[<15Y]=<15Y&fca[15%2B]=15%2B&fca[15-24]=15-24&fca[25-34]=25-34&fca[35-54]=35-54&fca[55%2B]=55%2B&tab=table
i am using python selenium with firefox and this is what i tried:
driver.set_page_load_timeout(30)
driver.get(url)
time.sleep(1)
WebDriverWait(driver, timeout=20).until(EC.presence_of_element_located((By.ID, 'SDG-Indicator-Dashboard')))
time.sleep(1)
download_div = driver.find_element(By.CLASS_NAME, 'float-buttons-wrap')
buttons = download_div.find_elements(By.TAG_NAME, 'button')
buttons_attributes = [i.get_attribute('title') for i in buttons]
download_button_index = buttons_attributes.index('Download')
buttons[download_button_index].location_once_scrolled_into_view
buttons[download_button_index].click()```
i keep getting the same error:
ElementNotInteractableException: Message: Element <button class="btn btn-outline-light btn-icons" type="button"> could not be scrolled into view
eventho i am getting the correct element and i tried using js like this:
```driver.execute_script("return arguments[0].scrollIntoView(true);", element)```
also did not work.
You have to modify the XPath, try the below code:
driver.get("https://data.unwomen.org/data-portal/sdg?annex=All&finic[]=SUP_1_1_IPL_P&flocat[]=478&flocat[]=174&flocat[]=818&flocat[]=504&flocat[]=729&flocat[]=788&flocat[]=368&flocat[]=400&flocat[]=275&flocat[]=760&fys[]=2015&fyr[]=2030&fca[ALLAGE]=ALLAGE&fca[<15Y]=<15Y&fca[15%2B]=15%2B&fca[15-24]=15-24&fca[25-34]=25-34&fca[35-54]=35-54&fca[55%2B]=55%2B&tab=table")
driver.implicitly_wait(15)
time.sleep(2)
download_btn = driver.find_element(By.XPATH, "(.//button[#type='button' and #title='Download'])[2]")
download_btn.location_once_scrolled_into_view
time.sleep(1)
download_btn.click()
1.You need to make sure you are giving enough time to complete page loading
2.Once you started to find button element , it's better to use try/catch blocks multiple times and put sleep function when exception occures to provide appropriate time to load scripts and elements.
3.Try finding by xpath instead of finding elements by indexes and be aware that you need to use the index [1] instead of [0] in a xpath string

Validating Pin-Pad form using Selenium Python

I am trying to input code on a pin pad form available on a website and get it validated. However, I am getting a validation error. I fear the clicked pin buttons are not being read by the website's script. Can anyone help me with this? I am attaching the code as well as the page source code.
The UI of website looks like this
The div class on website which contains the keypad is as follows:-
<div class="pin_pad">
<span id='keypad1' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">1</span>
<span id='keypad2' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">2</span>
<span id='keypad3' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">3</span>
<span id='keypad4' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">4</span>
<span class="submit_btn" onmouseout="this.className='submit_btn'" onmouseover="this.className='submit_btnpin_hover'" onclick="return validate();">Submit</span>
</div>
My pin is "4444". I am trying to use the .click() method to get "4" clicked. Although I can see that "4" has been clicked 4 times, upon submit, the page shows a validation error. Can anyone help me in solving this problem? Here's my code in python:-
pinpad = driver.find_elements_by_class_name('pin_pad')
element = driver.find_element_by_xpath("//span[text()=4]").click()
for k in range(4):
actionChains = ActionChains(driver)
actionChains.context_click(element).perform()
time.sleep(1)
driver.find_element_by_xpath("//span[text()='Submit']").submit()
Instead of
context_click(element).perform()
do this :
move_to_element(key4).click().perform()
context_click is for right click and move_to_element() is for moving your mouse cursor to the specify element.
Brief explanation :
You can introduce WebDriverWait for more stability, and I don't think you need to do context_click() instead it is move_to_element() plus ActionChains object creation optimization and instead of submit you may wanna use click(). Check out the code below :
pinpad = driver.find_elements_by_class_name('pin_pad')
key4 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#keypad4"))).click()
actionChains = ActionChains(driver)
for k in range(4):
actionChains.move_to_element(key4).click().perform()
sleep(1)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Submit']"))).click()

creating for loop using selenium python to click dropdown items

I am trying to create a for loop which:-
1) clicks the dropdown item
2) add a comment
3) submit a comment
This whole loop will happen in the same page itself. The XPath is correct and I can execute these lines individually once but it fails in a loop. What am I doing wrong here?
No particular error message given
add_comments = driver.find_elements_by_class_name('add')
comments = driver.find_elements_by_xpath("//form[#class='addComment expand']//textarea[contains(#placeholder,'Add a comment')]")
submit_comments = driver.find_elements_by_xpath("//button[text()='Comment']")
i = 0
for add_comment in add_comments:
for comment in comments:
for submit_comment in submit_comments:
WebDriverWait(driver, 5)
add_comment.click()
comment.click()
comment.send_keys("Awesome Art")
WebDriverWait(driver, 2)
submit_comment.click()
i += 1
if i > 4:
driver.close()
Link is https://society6.com/society?show=2 but might not work since its within my account. Here is the screenshot
The '+add comment is the part where i want to put comment
Html is here-
<form class="addComment expand" data-id="9647336">
<img src="https://ctl.s6img.com/society6/img/g2taHIrokQ01R_67jS8ulaWI2wk/h_150,w_150/users/avatar/~artwork/s6-original-art-uploads/society6/uploads/u/sul97/avatar_asset/d837ee10016843a3bba9ae3310cc338d" width="25" height="25">
<textarea placeholder="Add a comment..." data-button="9647336"></textarea>
<button id="b9647336">Comment</button>
</form>

How to switch to a modal in Python using Selenium

I'm using Python and Selenium. My problem is that I can't switch on the modal that pops out and I can't click the buttons in it.
This is the elements of the modal:
This is my code:
minus the url of course
browser = webdriver.Chrome(executable_path="D:\\sasdsa\\automate\\chromedriver_win32\\chromedriver.exe")
user_name = browser.find_element_by_xpath("//input[#id='username']")
user_name.send_keys("test.employee")
##Password
pass_word = browser.find_element_by_xpath("//input[#id='password']")
pass_word.send_keys("123")
##log_in = browser.find_element_by_css_selector(".btn")
log_in = browser.find_element_by_xpath("//button[#class='btn btn-sm btn-primary btn-block']")
log_in.click()
##punch
#driver.find_element_by_id("//#id='product_view')
#To open the modal
punch_in = browser.find_element_by_xpath("//button[#class='btn btn-success btn-sm pull-right']")
punch_in.click()
#cant switch to the modal to access the button
browser.switch_to_frame("product_view")
punch_in2 = browser.find_element_by_xpath("//button[#id='save_me']")
punch_in2.click()
Delete the line below and it should work fine.
browser.switch_to_frame("product_view")
You don't need to do anything special here. A modal dialog like this is just HTML like any other HTML on the page. You access it just like you would anything else.
Having said that... if you click a button, etc. that launches the dialog, you will probably have to add a WebDriverWait to wait for the dialog to be visible before accessing elements inside it, etc.
Andersson and JeffC are right. I had a similar issue. I treated the modal window as something different, which didn't work by the way. At the end, I just treated it in the usual way. I simply added browser.implicitly_wait(60) after initiating the browser, and it worked.
What Jeff said worked for me. I had
<div class="ReactModalPortal">
I just added sleep for 3 seconds to see if it was working. It worked.
Then I used :
act = ActionChains(self.driver)
act.send_keys(Keys.TAB).perform()
act.send_keys(Keys.ENTER).perform()

Interacting with website forms

I'm trying to connect to a school url and automate the process with selenium. Originally I tried using splinter, but ran into similar problems. I can't seem to be able to interact with the username and password fields. I realized a little ways in that it is an iframe that I need to interact with. Currently I have:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/")
driver.switch_to.frame('Content') #I tried contentFrame and content as well
loginid = driver.find_elements_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_elements_by_id('password')
passwd.send_keys("***")
sub = driver.find_elements_by_id('login')
sub.click()
time.sleep(5)
driver.close()
Here is the HTML that I am trying to interact with:
The Website: https://my.oregonstate.edu/webapps/portal/frameset.jsp
The iframe:
<iframe id="contentFrame" style="height: 593px;" name="content" title="Content" src="/webapps/portal/execute/tabs/tabAction?tab_tab_group_id=_1_1" frameborder="0"></iframe>
The forms:
Username:
<input name="user_id" id="user_id" size="25" maxlength="50" type="text">
Password:
<input size="25" name="password" id="password" autocomplete="off" type="password">
It seems that selenium can locate the elements just find, but I am unable to input any information into these fields, I got the error 'List object has no attribute'. When I realized it was the iframe I tried to navigate into that but it says 'Unable to locate frame: Content'. Is there another iframe that I am missing? Or something obvious? This is my first time here so sorry if I messed something up with the code linking.
Thanks for the help.
driver.switch_to.frame() takes frame's id or name, where your frame have id = contentFrame and name = content. (The reason they didn't work is probably because of a different issue, read through please)
First, please try use either one of them, not Content (which has upper case C).
Once you have fixed the issue above, there will be another error in your code.
loginid = driver.find_elements_by_id('user_id')
loginid.send_keys("***")
driver.find_elements_by_id finds all matching elements, which is a list. So you can't use send_keys. Please use driver.find_element_by_id('user_id').
Here is the code I tested working.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/")
driver.switch_to.frame('content') # all lower case to match your actual frame name
loginid = driver.find_element_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_element_by_id('password')
passwd.send_keys("***")
Regarding issue in your following comments
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://my.oregonstate.edu/webapps/login/?action=relogin")
loginid = driver.find_element_by_id('user_id')
loginid.send_keys("***")
passwd = driver.find_element_by_id('password')
passwd.send_keys("***")
driver.find_element_by_css_selector('.submit.button-1').click()

Categories