Python Selenium Click Next Button - python

I need to click the "next" button on the following page:
https://www.amazon.com/gp/goldbox/ref=gbps_ftr_s-4_bedf_wht_29726380?gb_f_deals1=dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL%252CEXPIRED%252CSOLDOUT%252CUPCOMING,includedAccessTypes:,sortOrder:BY_SCORE,enforcedCategories:2972638011&pf_rd_p=afc45143-5c9c-4b30-8d5c-d838e760bedf&pf_rd_s=slot-4&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=60XTK6YDM9WEAFXB6519&ie=UTF8
I have the following code that will not find xpath element for the "next button":
browser.find_element_by_xpath("//ul[#class='a-pagination']/li[#class='a-last']/a").click() # Click on next button
I have also tried this variation:
browser.find_element_by_xpath("//div[#id='pagination-next-6885645543374035']/ul/li[#class='a-last']/a")
I also tried directly copying the xpath from the inspector. Neither of these three options work.
Thanks in advance for your help.

The issue is Amazon attaches a numerical ending onto the ID that is different each time the page loads. Finally found a way to select it using xpath:
browser.find_element_by_xpath("//span[#class='a-declarative']/div[2]/ul/li[#class='a-last']/a")

Related

How to click on a button that does not appear on page source?

I'm trying to click on a button using Selenium with Python, however the button does not appear on Page Source and thus, the instantiated driver cannot find it. Here is the link I am accessing: https://data.ntsb.gov/carol-main-public/query-builder?month=6&year=2021
And the button I want to click is "Download Summary (CSV)", which is located at the end of the page.
I have already googled this kind of trouble but I haven't found a possible solution so far.
My attempt was to find the button directly by using the following commands, but it returns a NoSuchElementException:
element = driver.find_element_by_id('exportSummaryButton')
element.click()
This exception triggered me the intuition that the page source differs from what I see on Devtools and this is exactly what happens.
Thanks in advance!

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.

How to Find an Element to Click on Instagram Unfollow Button

I am making an Instagram bot using selenium and python. I am trying to implement a function which allows a certain amount of followers to be unfollowed. The code navigates to the unfollow screen which has many buttons that say unfollow. The buttons are hard to find with selenium though. If the find_element_by_tag('button') function is used only 3 buttons are found. The button html is:
<button class="_0mzm- sqdOP L3NKy _8A5w5 " type="button">Following</button>
For some reason, I cannot get selenium to select the element. Are there any ways someone can see how to click it?
Try switching to the active element and then clicking on the following
driver. switchTo() . activeElement() ;
And then try the xpath that you tried. This should work
Copy the xpath of the button tag and create reference and use for loop to iterate from one unfollow button onto the next, see my github repository
https://github.com/asadamatic/Unfollow-All-On-Instagram/blob/master/unfollowAll.py

How to click on button using several conditions

I try to press a button using selenium. How do I click on a particular button using several conditions? (https://ibb.co/cJZxD7b) describing image
I have already tried something like this //span[text()=1.01], but what if webpage have few buttons with the same text.
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='45.00 7.75 1.04'] and //span[text()=1.04"))).click()
I expect to click on particular button
You can try with xpath
//span[#class='stn-val']
But it would probably be the best if you shared more of the HTML code because now I'm only guessing based on what I see.
Also, are you getting any error messages?
UPDATE:
Based on HTML posted in comments, use this selector:
//div[#class="st-col-bet-container"][2]/ul/li[3]/span
For tips on xpath selectors, see here.

Categories