How to click an image given an Xpath in Selinum with Python? - python

So I have the HTML code:
enter image description here
How do I click this button? What is the Xpath for this one?

The xPath to that particular image is
/html/body/img
To click it
driver.findElement(By.xpath("/html/body/img")).click()

For xpath you need to have a path from top level upto this button.
Would look something like html\body\div\button [ just an example ].
You will have to check the whole page to navigate to that button using xpath.
Another better way could be using find element by class="auth-button positive" and then click() it

Related

Can't find the right XPATH for python locator

I write locators, need to find the checkbox by the text of the span element. Tried this one but didn't work
//span[text()='#1599064']/ancestor::div[#data-class-name='SourceView']/div/input
Please help
https://i.stack.imgur.com/Hljbi.png
With only this information is difficult, but if the text is static you can try to locate that one using the developer console. If you're on Chrome/Firefox press F12, then under the Elements tab (on chrome) click the exact element you would like to extract the XPATH selector, then right click and Copy > Copy Xpath.
I hope this can help you .

Click a Button with Python Using Selenium

I have tried all the grabbing methods and I was not able to click on Not Now.
Xpath never worked, CSS selector..etc
enter image description here
You can try to click by text in the button, something like this:
driver.find_element_by_xpath("//button[contains(string(.),'Not Now']").click()

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.

Python grab element in scroll box

How can I grab an element in a scroll box based on the text?
I'd like to click the shoe " NAVY/NAVY/GREY (444)". I've tried
`color_code = '444' `
`driver.find_element_by_partial_link_text(color_code)`
Here's the source for the website I'm trying to navigate through
Highlighted is the element I'd like to grab and click()
Any help/insight would be appreciated.
Link text and partial link text only work on anchor tags <a>. Since your element is a <span> you might want to use css selector or xpath to locate it. If you specifically want to click on the shoe with that colour code, you can use driver.find_element_by_xpath("//span[contains(text(), color_code)] provided the page only has one shoe with that colour code.
You can also use find_elements_by_xpath method to get a list of all the shoes with that colour code and iterate over it to perform actions
try this :
driver.find_element_by_css_selector("span.btn-xs.btn-link.addProductColor").click

Python Selenium Click Next Button

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")

Categories