How to click a button in selenium python - python

I am using Python script to open a web page, then a tab and then i want to click on a button. I am stuck on the last part. I am unable to click the find button. Here is the HTML code when i am using inspect in chrome.
input value="Find" class="cuesButton" name="findButton"
onclick="javascript:onFindSubmit()" type="button"
Here is the button i am trying to click:
I tried driver.find_element_by_name, element_by_id. It says method css selector doesnt have this element by name but it still fails.

2 things can be happening, 1 the page isn’t finished loading, to wait use WebDriverWait like so:
wait = WebDriverWait(driver, 10)
Or 2, the element is in an iframe , in this case you would need to switch to it like so:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
You can switch using xpath, name, cssselector, and tag name

Related

How to get element name or id in bootstrap carousel next button for Selenium Automation?

I am trying to automate bootstrap carousel next button with Selenium.
The code is given as follows:
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='d:/CRD/chromedriver_win32/chromedriver.exe')
driver.get('file:///D:/slider/bootst.html')
button1 = driver.find_element(By.CLASS_NAME, "a.carousel-control")
button1.click()
The way I am trying to get the ID using chrome inspect element option as shown below:
How can I get the next button pressed automatically using Selenium.
You can use other selectors like XPATH if you want, but I think you should use CSS SELECTOR for "a.right.carousel-control" not CLASS_NAME.
More explanation on stackoverflow
Have a great day !

Why won't Selenium in Python click the pop-up privacy button?

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.

insert data into tag with selenium

I have a page where I can't click on a button with selenium the html look like this before clicking into button:
And that's how is look like after pressing the button:
Is have a way to insert that values into the html tag to get visibility of the element I want in the case button.I try with WebDriverWait and ActionChains but no success.
I execute the javascript code:
driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatelement", "button")')
driver.execute_script('document.getElementsByTagName("html")[0].setAttribute("data-whatclasses", "Button__StyledButton-iESSlv,dJJJCD Button-dtUzzq kHUYTy")')
And still the button element is not visible.How can I get the element try with EC wait and action chain.Is it timeout exception.Any advice.
Using execute_script you can run javascript and manipulate the DOM at will. See: Running javascript in Selenium using Python
So, for example (I'm not entirely sure what you need to do to get visibility of the element), to re-insert the data-inq-observer attribute into the body tag:
driver.execute_script('document.getElementByTagName("body").setAttribute("data-inq-observer", "1")')

Automate clicking button using python and selenium

I would like to automate clicking on "Refresh" button (See image "Refreshbutton") using python and selenium. I have tried to locate the button using xpath, id, class name but each time, I get the NoSuchElement exception, i.e. it cannot find the element. How can I locate the Refresh button? I don't care if it is by xpath, id or any other means. Thanks in advance!
HTML
RefreshButton
Find by XPATH try
Find By ID try
Class Name try
Python Code Snippet
The problem might not be the id/xpath but the presence of the button on the page. If the click is executed before the HTML page is fully loaded, the element might not be here yet. To make sure, you can use the WebDriverWait as follow:
element_xpath = '//*[#id="btnRefreshCameraList"]'
wait_time = 15 # wait for page to load for max. 15 seconds
WebDriverWait(
driver, wait_time).until(
EC.presence_of_element_located((By.XPATH, element_xpath))
)
)
driver.find_element_by_xpath(element_xpath).click()
Didn't have access to your site, so I couldn't try it out.

Python Selenium button click

I am trying to click a button to show more comments on a discusson thread on http://disqus.com/
The HTML looks like following:
<div class="load-more" data-role="more" style="">
Show more
</div>
I have tried using the xPath from the button like following:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.get(url)
driver.find_element_by_xpath('''//*[#id="posts"]/div[3]/a''').click()
But I get the NoSuchElement exception.
What is the proper way of clicking a button with that type of HTML?
UPDATE: Worked when I switched to a specific iFrame:
driver.switch_to.frame('myID')
Then loaded by class name:
element = driver.find_element_by_class_name('load-more')
element.click()
NOTE: That the click() did not work when it was performed on the same line like driver.find_element_by_class_name('load-more').click()
Xpath should be a last resort for finding elements. For your case, I would try
driver.find_element_by_class_name("load-more__button"));
Using btn load-more__button won't work as compound classNames aren't valid.
If that still doesn't work, Id recommend using css selectors before Xpath.
However, if none of these options work, and your Xpath is still not working, you should use the firebug add on for Firefox to ensure the Xpath you are using is correct.
Here's some of my other answers that may help you get started with Firebug:
https://stackoverflow.com/a/38980488/3537915
https://stackoverflow.com/a/38723782/3537915
https://stackoverflow.com/a/38744577/3537915

Categories

Resources