How to move selenium webdriver to next page after clicking submit button? - python

So I got to the point where my program fills in the textbox and clicks the proceed with booking button with a submit() command, however in the html there is no url to put in the driver.get([url]) method to move my driver to the next page.
All subsequent methods called on driver are on the "Proceed with booking" page instead of the next page it takes me to. How do I continue the program by moving my driver to the next page?
My code:
#types in studentID
studentIDBOX = driver.find_element(By.NAME, 'MEMBER_NO')
studentIDBOX.send_keys('12345678') #example ID
#submits the proceed to booking button
proceedBox = driver.find_element(By.XPATH, '//*[#id="single-column-content"]/div/div/div/div[2]/div/form/input[5]')
proceedBox.submit()
# WHAT DO I ADD TO DRIVER.GET??

proceedBox.submit() should submit the <form> and take you to the loggedin page. You won't have to explicitly invoke driver.get(url). Once the new page loads you can as usual invoke the following commands:
find_element()
find_elements()

Related

Unable to click elements on Selenium webdriver using Python

Writing a test script for the new balance website, and for some reason, I am unable to click certain buttons, I am getting an exit code 0 when my script is done but I know that the buttons have not been selected when I sleep the web driver, it shows me that the action has only located the element but not clicked them.
# Driver select the first trainer option visible on the page
driver.find_element(By.XPATH,"//img[#title='Fresh Foam X 1080v12, M1080Z12']").click()
# Driver click on the orange version of these trainers
driver.find_element(By.XPATH,"//button[contains(#title,'M1080M12')]//span[contains(#class,'p-auto')]").click()
# Make sure you have the correct colour
trainerColour1 = driver.find_element(By.XPATH,"//span[#class='display-color-name color-name-mobile font-body regular pdp-update-event-triggerd']").text
print (trainerColour1) # For some reason its not printing this element on the log
#assert "apricot" in trainerColour1
# Pick a size 8 of the shoe
driver.find_element(By.XPATH,"//span[normalize-space()='8']").click()
driver.implicitly_wait(5)
# Add it to the cart
checkoutButton = driver.find_element(By.XPATH,"//button[#class='add-to-cart nb-button button-primary button-full-width']")
actions = ActionChains(driver)
actions.move_to_element(checkoutButton)
actions.click(checkoutButton)
actions.perform()
cartButton1 = driver.find_element(By.XPATH,"//a[#title='Cart 0 Items']//*[name()='svg']")
actions.move_to_element(cartButton1)
actions.click(cartButton1)
actions.perform()
# Go on view cart
driver.implicitly_wait(7)
time.sleep(15)
I am getting exit code 0 but the problem is, because these elements have not been clicked I cant checkout and finish my test case. I was expecting the webdriver to click the add to cart button and then click the checkout button on the top right but unfortunately this isnt happening.
Getting exit code Page and buttons I am failing to automate
New Balance page im trying to automate

Python Selenium: Click Instagram next post button

I'm creating an Instagram bot but cannot figure out how to navigate to the next post.
Here is what I tried
#Attempt 1
next_button = driver.find_element_by_class_name('wpO6b ')
next_button.click()
#Attempt 2
_next = driver.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
Neither of two worked and I get a NoSuchElementException or ElementClickInterceptedException . What corrections do I need to make here?
This is the button I'm trying to click(to get to the next post)
I have checked your class name coreSpriteRightPaginationArrow and i couldn't find any element with that exact class name. But I saw the class name partially. So it might help if you try with XPath contains as shown below.
//div[contains(#class,'coreSpriteRight')]
another xpath using class wpO6b. there are 10 elements with same class name so filtered using #aria-label='Next'
//button[#class='wpO6b ']//*[#aria-label='Next']
Try these and let me know if it works.
I have tried below code and it's clicking next button for 10 times
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
if __name__ == '__main__':
driver = webdriver.Chrome('/Users/yosuvaarulanthu/node_modules/chromedriver/lib/chromedriver/chromedriver') # Optional argument, if not specified will search path.
driver.maximize_window()
driver.implicitly_wait(15)
driver.get("https://www.instagram.com/instagram/");
time.sleep(2)
driver.find_element(By.XPATH,"//button[text()='Accept All']").click();
time.sleep(2)
#driver.find_element(By.XPATH,"//button[text()='Log in']").click();
driver.find_element(By.NAME,"username").send_keys('username')
driver.find_element(By.NAME,"password").send_keys('password')
driver.find_element(By.XPATH,"//div[text()='Log In']").click();
driver.find_element(By.XPATH,"//button[text()='Not now']").click();
driver.find_element(By.XPATH,"//button[text()='Not Now']").click();
#it open Instagram page and clicks 1st post and then it will click next post button for the specified range
driver.get("https://www.instagram.com/instagram/");
driver.find_element(By.XPATH,"//div[#class='v1Nh3 kIKUG _bz0w']").click();
for page in range(1,10):
driver.find_element(By.XPATH,"//button[#class='wpO6b ']//*[#aria-label='Next']" ).click();
time.sleep(2)
driver.quit()
As you can see, the next post right arrow button element locator is changing between the first post to other posts next page button.
In case of the first post you should use this locator:
//div[contains(#class,'coreSpriteRight')]
While for all the other posts you should use this locator
//a[contains(#class,'coreSpriteRight')]
The second element //a[contains(#class,'coreSpriteRight')] will also present on the first post page as well, however this element is not clickable there, it is enabled and can be clicked on non-first pages only.
As you can see on the picture below, the wp06b button is inside a lot of divs, in that case you might need to give Selenium that same path of divs to be able to access the button or give it a XPath.
It's not the most optimized but should work fine.
driver.find_element(By.XPATH("(.//*[normalize-space(text()) and normalize-space(.)='© 2022 Instagram from Meta'])[1]/following::*[name()='svg'][2]")).click()
Note that the XPath leads to a svg, so basically we are clicking on the svg element itself, not in the button.

Refreshing the source code without refreshing the web page

I want to measure the number of cookies regarding of the policie accepted by the user.
So, for example, one the website https://sephora.fr i'm first accessing to the control panel :
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)
driver.get(website)
driver.find_element_by_id('footer_tc_privacy_button').click()
Then i would like to click on the black button ("Enregistrer")
driver.find_element_by_id('save-consent').click()
The problem is that the HTML code source is updated after the first click and selenium keep the initial source code -> selenium can't find the button 'save-content'.
Unfortunately i can't refresh the page because it will close the control panel.
I tried to sleep some seconds after the first click, but it's not working.
Any idea ?
Edit : i also tried to switch to the Iframe
frame = driver.find_element_by_xpath('//frame[#name="privacy-iframe"]')
driver.switch_to.frame(frame)
because the button Enregistrer is on this iframe
<iframe id="privacy-iframe" class="tc-reset-css tc-privacy-center-iframe" src="https://cdn.trustcommander.net/privacy-center/default/modern/index.html" title="Vos paramètres cookies" lang="fr"></iframe>
but it's not also not working
EDIT : Solution
I switched to the iframe
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)
driver.find_element_by_id('save-consent').click()
then i switched back to the parent
driver._switch_to.parent_frame()
I do not see the name in the iframe tag attribute shared by OP :
try with ID instead :
frame = driver.find_element_by_id('privacy-iframe')
driver.switch_to.frame(frame)

How to access elements of page after login selenium?

I can successfully login using the selenium web driver, but I don't know how to access the frame on the next page. I have tried setting the new frame, but it does not find that element because I think it is looking through the elements on the login page instead of the page after. This is the url after the login is successful. https://homeaccess.katyisd.org/HomeAccess/Classes/Classwork
Path of frame on page AFTER login page
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = "https://homeaccess.katyisd.org/HomeAccess/Account/LogOn?ReturnUrl=%2fHomeAccess"
browser.get(url)
browser.find_element_by_id('LogOnDetails_UserName')
browser.find_element_by_id('LogOnDetails_Password')
browser.find_element_by_id('LogOnDetails_UserName').clear()
browser.find_element_by_id('LogOnDetails_Password').clear()
browser.find_element_by_id('LogOnDetails_UserName').send_keys('******')
browser.find_element_by_id('LogOnDetails_Password').send_keys('******')
This is all on the page after the login
frame = browser.find_element_by_xpath('//*[#id="sg-legacy-iframe"]') //prints no such element found
browser.switch_to_frame(frame)
browser.find_element_by_xpath('//*[#id="SignInSectionContainer"]/div[2]/button').click()
Try to wait for frame to appear:
frame = WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'sg-legacy-iframe')))
After performing click on Login please wait for some time to load the page.
You can use WebDriverWait for waiting for required element
if above one fails generally in java in will go for Thread.sleep(5000);
As per provided HTML code you can try switching to frames by other locators if id fails
driver.switchTo().frame("sg-legacy-iframe"); //by providing id
driver.switchTo().frame(driver.findElement(By.xpath("//div[#id='MainContent/iframe"))); //by providing xpath
Thank You,
Murali

How to click an invisible element with selenium?

i used this code to check splinter's clicking button option:
from splinter import Browser
with Browser() as browser:
# Visit URL
url = "http://www.google.com"
browser.visit(url)
browser.fill('q', 'splinter - python acceptance testing for web applications')
# Find and click the 'search' button
button = browser.find_by_name('btnG')
# Interact with elements
button.click()
if browser.is_text_present('splinter.readthedocs.org'):
print("Yes, the official website was found!")
else:
print("No, it wasn't found... We need to improve our SEO techniques")
and i got exception:
Element is not currently visible ans so may not be interacted.
waiting for the browser is not the solution (becuase i made sleep method for long time and still doesnt work).
this is sample code shown in https://splinter.readthedocs.org/en/latest/#sample-code , but is doesnt work for me
If you want to wait for an element to become invisible, you can use wait function:
wait = WebDriverWait(self.driver, 30)
wait.until(EC.invisibility_of_element_located((By.XX, "something")))

Categories