HTML code for the button I want to click
Can anyone please help me, I'm trying to tell selenium to click a button on a website I'm trying to automate. Ive attached a picture of the HTMl code that I want the Webdriver to click.
Can anyone tell me the best find element option to use to click the button.
Ive looked into Xpath but can't seem to get it working?
Thanks
<use xlink:href="#icon-menu-more" href="#icon-menu-more"></use>
So I was trying to grab the xPath from this line of code.
the Xpath is /html/body/div/div[1]/header/nav/button[1]/span[1]/svg/use
but then when trying to use driver.find_element_by_xpath() i get an error saying unable to locate element.
You can try
driver.find_element_by_id('icon-menu-more')
this will help you to click the button
Other Options are
driver.find_element_by_name('name of element')
driver.find_element_by_class_name('class name of element')
driver.find_element_by_tag_name(tag name of element)
driver.find_element_by_css_selector('css selector of element')
Related
I'm trying to click on the See Story button where (you need to be logged in): https://www.instagram.com/stories/inoxtag/
Button HTML (with French text):
<button class="_acan _acap _acau _acav" type="button">Voir story</button>
I'm using Selenium and Python.
My code is:
driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button").click()
And it leads me to this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button"}
I'm using xpath because the button class seems to be obfuscated, so I prefer to do it by xpath.
I know my xpath is correct because if I do F12 on the webpage, then paste my xpath I have my button selected.
Thank you
So I debugged your problem. When you open (driver.get(...)) the link, have you taken into account all the "Allow essential and optional cookies", and then logging in into your account?
If you have debugged this in incognito mode and kept the window open, incognito would have stored your data and so wouldn't ask you for your details again.
But if you debug the script and just put a break point on the line
driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/div[1]/div/section/div/div[1]/div/div/div/div[3]/button").click()
Then you proceed to fill your data until it routes you to the page you showed and then you try to execute the code that clicks the button you wouldn't have a problem.
I'm trying to click buttons on amazon with selenium (python) but it won't work. It says the elements don't exist. I tried to google it but only found outdated solutions. I've tried Xpaths,ID,full XPATH on pretty much all input fields with no success. I've used selenium before and it works flawlessly but on amazon in particular it can't find the elements. For example the place order button (Je bestelling plaatsen in my language) can't be found nor clicked.
driver.find_element(By.XPATH, '//*[#id="turbo-checkout-pyo-button"]/span/input')
driver.find_element(By.ID, 'turbo-checkout-pyo-button')
driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
How do I know which elements are clickable and direct to the intended page? If there is a way to tell please let me know. Thanks in advance
I'm not a selenium master but you can use the find_element_by_xpath function that returns an element object that can be clicked so:
element = driver.find_element_by_xpath('/html/body/div[4]/div[1]/div/div/div/div[2]/div/form/div/span/span/span/input')
element.click()
The problem was that the button was located in an iframe. Switching to the iframe and then pressing the button does the job. Found out through this question
driver.switch_to.frame("turbo-checkout-iframe")
directButton = driver.find_element(By.CSS_SELECTOR, '#turbo-checkout-pyo-button')
directButton.click()
I am having some issues with Selenium not clicking the pop-up privacy button on https://www.transfermarkt.com/
Here is my code so far:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
It comes up saying:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[2]/div[3]/div[2]/button"}
Anyone have any thoughts?
I believe the issue is that the element you are trying to click is inside an iframe. In order to click that element you'll have to first switch to that frame. I noticed the iframe has title="SP Consent Message" so I'll use a CSS Selector to identify it based on that. Your code with the added line:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.transfermarkt.com/')
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title="SP Consent Message"]'))
accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button')
accept_button.click()
Note that you may have to switch back to the default frame to continue your test, I'm not 100% sure as that iframe has gone away.
Also it seems like some folks don't get that popup when the hit the website, not sure why, may be something you want to look in to to determine how you want to test this.
Like #Prophet says you should improve the xpath for the button (again it seems to have a unique title so I would use CSS Selector 'button[title="ACCEPT ALL"]'), but this works for me to click it.
There are 2 issues here:
You need to add wait / delay before accept_button = driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div[2]/button') to let the page load
It's very, very bad practice to use absolute XPaths. You have to define an unique, short and clear related XPath expression.
Additionally, I see no pop-up privacy button when I open that page. So, maybe that element is indeed not there.
I am trying to check whether the 'Add to cart' button exists or not on the webpage [Ebuyer][1]
Using Chromedriver.
I have tried a few things including:
driver.find_element_by_xpath("/html/body/section/div[1]/div[3]/div[1]/div[3]/form/input[2]")
driver.find_element_by_class_name("button--add-to-basket")
The problem is that it just cant seem to find the button, any help will be greatly appreciated!
[1]: https://www.ebuyer.com/809813-corsair-ll-series-ll120-rgb-120mm-3-fan-pack-co-9050072-ww?_sgm_campaign=scn_8714e1eb7e000&_sgm_source=809813&_sgm_action=click
If you mean Add to Basket button, use CSS Selector:
driver.find_element_by_css_selector(".js-add-to-basket-main.js-show-loader")
Using css selector works.
css = 'input.button.button--add-to-basket' driver.find_element_by_css_selector(css)
I'm trying to find and click an element with this command:
driver.find_element_by_xpath('//*[#id="address-book-entry-0"]/div[2]').click()
But it doesn't work.
For other buttons it's working, why for this it is not working?
Can you help me please?
You should show the relevant part of the HTML in order to be sure, but this behaviour could be caused by:
Wrong XPath.
The element is not clickable. You can wait until it is clickable. Check again that there is no other element on top of yours that will receive the click.
The element is in iframe. Switch to the iframe first.
If the suggestions above are not working for you, try to click the button with JavaScript.