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
Related
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()
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.
I am trying to loop clicks with Selenium,
the first click is working but the second gives error below:
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
Here is the code:
def send_keys_dropdown(self,url):
bot = self.bot
bot.get(url)
elements = bot.find_elements_by_xpath("//a[#data-tag='globalize']")
for elem in elements:
class_of_element = elem.get_attribute("class")
if class_of_element == 'CsLinkButton':
elem.click()
time.sleep(5)
# close icon
bot.find_element_by_xpath("//a[#data-tag='cmdCancel']").click()
How I can click these elements inside loop? Thank you!
It seems after click action performed element may be inside scroll down or up window form.
following may help you please go through it.
after click action performed add following line.
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",
elem);
here, "elem" is web element of your click button.
Having serious issues here. Someone please help.
I am trying to login to a website. - This Works!
Redirect to the page I want after Login - This Works!
Now once in, I have to hover over the settings icon so the dropdown shows, and then click on the "Settings" options that has NO ID or CLASS or HREF.
Now there is a couple of reasons I cant do this. Number 1 is if I try to click on the link after the hover, it tells me that it's hidden and I cant interact with it. Also the menu options in the form are populated and appended once you hover, through ajax I think. They are not on the initial page load.
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
ActionChains(driver).move_to_element(menuButton).perform()
After the above code is deployed I print the driver.page_source and can see (below) that the new menu options are there, if you don't hover, the below code will not be on the page.
Now the <a> i'm trying to click is the <span>Settings</span> option, and for the life of me, it will not work. Either can't find it, not clickable, can't interact with it, etc, etc, etc. xpath, css_selector, partial_name, nothing finds this thing. Also whats weird is once you click on it, from a browser, it appends an ID to <span> So weird. Any ideas?
<a data-ylk="rspns:nav;t3:tl-lst;t5:custitm;slk:custitm;elm:itm;elmt:custitm;itc:0;cpos:2" class="C(#000)! Td(u):h " data-mad="options" data-customevt="true" href="#" data-rapid_p="18"><span>Settings</span></a>
To perform mouse over event on element you should try to use .execute_script() using following java script :-
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "yucs-help_button")))
menuButton = driver.find_element_by_id("yucs-help_button")
driver.execute_script("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('mouseover', true, true); arguments[0].dispatchEvent(clickEvent);", menuButton)
Now after successfully mouse over you should try to click on Settings link as below :-
driver.find_element_by_xpath("//span[contains(text(), 'Settings')]/parent::a[#data-mad = 'options']").click()
Hope it will help you..:)
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")))