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.
Related
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 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
I am having a hard time locating the play element on a mobile version of a webpage (my python script is passing a mobile user-agent in the header.)
the website url is below (NOTE: must be accessed with a mobile user-agent else it won't show the correct page and reverts to standard browser page instead)
https://m.soundcloud.com/mbmproductions/sets/mark-berrys-playlist
Using inspect in the browser, I have been unable to work out what exactly is needing to be clicked to start it playing. The entire image area around the play button seems to work if I click on it manually, but no element listed by inspect seems to work when using the script to start it playing.
Can anyone explain how to find the correct clickable element in this link so I can add it to a script in order to click it and start it playing. Ideally using XPATH as per my code below, but class or ID or anything would do if it works, even javascript if I have to.
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, targetelement))
)
element.click()
I make some amendment to your script. Use action instead of click(). Change clickable explicit to presence.
element = WebDriverWait(driver, 10).until(
EC. presence_of_element_located
((By.CLASS_NAME, ‘g-header-artwork-controls’))
)
el = driver.find_elements_by_css_selector(“.playlist__playButton.g-play-button")
webdriver.ActionChains(driver).move_to_element(el).click(el).perform()
I'm trying to write code in python that can control an embedded video player (jwplayer) through an automated browser, specifically Smores.tv. (https://smores.tv/watch.php?v=j5UPZpjO&p=Yw4cOKqt) However, when I try to locate the element of the pause/play button or any of the controls it returns a nosuchelement exception.
I've done some research into this, and it seems like the problem could be that the video player is inside an iframe, which i was able to find:
frame = driver.find_element_by_id("jwp-global-frame")
driver.switch_to.frame(frame)
did not work and the error was still NoSuchElementException. There may be multiple embedded iframes or something similar, but i'm stuck on this issue.(also I am aware of the issue that could come from the page not loading, and since I couldn't find an element for an explicit wait I am currently using an implicit wait for the page to load)
frame = driver.find_element_by_id("jwp-global-frame")
driver.switch_to.frame(frame)
pausebutton = driver.find_element_by_xpath('//*[#id="player"]/div[9]/div[4]/div[2]/div[1]')
pausebutton.click()
In the example site, the play button is not inside any Iframe.
So I have tried without switching to iframe and it worked for me.
# removed the iframe lines
pausebutton = WebDriverWait(driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//[#id="player"]/div[9]/div[4]/div[2]/div[1]')))
pausebutton.click()
You can also find that element with css selector as .jw-icon-playback