I've been trying to click a button that downloads a CSV file from "https://mol.org/regions/?regiontype=countries". I'm sure that I've selected the button, as I can print the text written on it, but whenever I try to .click() it, it doesn't download the file. Are there any additional steps needed to operate the function bound to the button? Thank you in advance.
PS : The button works manually.
Here is the driver code I used :
with webdriver as driver:
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, search_bar_CSS_Selector))).send_keys(search_query+Keys.RETURN)
driver.find_element_by_css_selector(download_button_CSS_Selector).click()
print(driver.find_element_by_css_selector(download_button_CSS_Selector).text)
driver.close()
You can see that I actually print the button text & can access it, but the .click() is not working as expected.
Variables :
search_query = 'Egypt'
search_bar_CSS_Selector = "input[placeholder='Filter Political boundaries']"
download_button_CSS_Selector = "button[ng-click ='initiateDownload()']"
Your css selector looks perfect, but I think it's a page loading issue. So I tried that with an explicit wait command (check below) and it seems working fine.
Sample code :
so instead of this :
driver.find_element_by_css_selector(download_button_CSS_Selector).click()
use this :
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()
Update 1 :
driver.get("https://mol.org/regions/?regiontype=countries")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Filter Political boundaries']"))).send_keys('Egypt'+Keys.RETURN)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()
Related
I'm trying to get Selenium to automate uploading and downloading files from https://8mb.video/ I can upload the file just fine, but after it processes on the site Selenium can't locate the element for the download link even though the ID given matches the ID in the html. Here's my code:
driver = webdriver.Edge()
driver.get('https://8mb.video/')
driver.maximize_window()
driver.get("https://8mb.video/")
s = driver.find_element(By.XPATH, "//input[#type='file']")
s.send_keys("C:\\Users\\ijwto\\Desktop\\VUT\\bladee.mp4")
s = driver.find_element(By.ID, "rockandroll")
s.click()
try:
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "dllink"))
)
finally:
print("nope")
I've also tried using element_to_be_clickable which didn't work, and checked for iframes in the HTML and didn't find any.
Any help would be greatly appreciated.
In order to download the file need to click on the element in the try block
Also if the intention of printing Nope in the finally block is to indicate if the element was not found then it can be added under except instead of finally
Note:- The wait time for WebDriverWait may increase in case the video you are trying to upload is large and the site requires more time to process it
Your solution would like
driver = webdriver.Edge()
driver.get('https://8mb.video/')
driver.maximize_window()
driver.get("https://8mb.video/")
s = driver.find_element(By.XPATH, "//input[#type='file']")
s.send_keys("C:\\Users\\ijwto\\Desktop\\VUT\\bladee.mp4")
s = driver.find_element(By.ID, "rockandroll")
s.click()
try:
element = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "dllink"))
)
element.click()
except:
print("Nope")
Question:
How can I show all reviews by clicking "Show more reviews" button?
What did I do:
To scrape all reviews, I decided to Keep clicking until that button disappears.
But some new reviews didn't appear after clicking for 8 times (using while below).
I already checked xpath of the button but it didn't changed.
If I click the button manually without the driver, I can see new reviews.
import time, random
from selenium import webdriver
from latest_user_agents import get_random_user_agent
### ser user_agent
user_agent = get_random_user_agent()
options = webdriver.ChromeOptions()
options.add_argument("--user-agent=" + user_agent)
driverpath = "C:/Users/~~/chromedriver.exe"
driver = webdriver.Chrome(chrome_options=options,executable_path=driverpath)
### target url
url = "https://www.sony.co.uk/electronics/truly-wireless/wf-l900/reviews-ratings"
### open URL
driver.get(url)
time.sleep(5)
# accept cookies
driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]').click()
# show all reviews
try:
while True:
driver.execute_script('window.scroll(0,1000000);')
driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button').click()
time.sleep(random.randrange(2, 5, 1))
except:
print("---------------- finish showing all reviews ----------------")
Try going to the end of the webpage and then do it as the button might not been loaded fully.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
You might also try to press the button only when it is available.
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))
You can try javascript click:
element = driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button')
driver.execute_script("arguments[0].click();", element)
In this method, the scrolling shouldn't be necessary. See more info about the subject here.
I'm using Selenium to click the "Become A Member" button from this link: https://www2.hm.com/en_us/register.
Here is the HTML of the button: https://i.stack.imgur.com/Pjeu3.png
I have exhausted all other answers on this site: I have tried to find this element using XPath, CSS Selector, waited for element to be clickable, visible, etc. all but to no avail.
Here is my current code that accepts all the cookies (since I thought that was the problem) and then tries to click on the "Become a Member" button
try:
# Accepts cookies
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='onetrust-accept-btn-handler']"))).click()
# Clicks the register button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-testid='submitButton']"))).click()
except:
print("Driver waited too long!")
driver.quit()
Does anyone know what I can do to fix this issue? Thank you!
to click on Become A Member button you are using input[data-testid='submitButton'] which is almost correct but it's not input tag, it's a button.
See the HTML here :
<button class="CTA-module--action__3hGPH CTA-module--medium__dV8ar CTA-module--primary__3hPd- CTA-module--fullWidth__1GZ-5 RegisterForm--submit__2Enwx" data-testid="submitButton" type="submit"><span>BECOME A MEMBER</span></button>
so changing input[data-testid='submitButton'] to button[data-testid='submitButton'] did the trick and it worked.
Sample code : -
try:
# Accepts cookies
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='onetrust-accept-btn-handler']"))).click()
# Clicks the register button
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid='submitButton']"))).click()
except:
print("Driver waited too long!")
driver.quit()
I try clicking on the button search but this is not work.
Here is the top code :
driver = webdriver.Chrome()
url = "https://www.flashscore.fr/"
driver.get(url)
the code that problem (first version):
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search")))
driver.execute_script("arguments[0].click();", buttonSearch)
This code does not what is asked but does not display an error.
So I have chosen a class in the class header__button--search named searchIcon___2C7g3ZD
Here it is:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
driver.execute_script("arguments[0].click();", buttonSearch)
It display this error : selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function
Why is it not possible of click on the button search from page ?
EDIT :
here is code HTML of button search on the page :
<div id="search-window" class="header__button header__button--search"><span class="searchIcon___HyESXZA"><svg class="searchIcon___2C7g3ZD "><title>Search</title><use xlink:href="/res/_fs/build/control-icons-symbols.0acb5c0.svg#icon--search"></use></svg></span></div>
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search"))) driver.execute_script("arguments[0].click();", buttonSearch)
This does not work because you didn't call any method on buttonSearch. For example methods such as buttonSearch.click(), or buttonsearch.doubleClick(), etc. Without calling a method, the error will not be displayed. This is because instead of using the css selector of the element you instead used it's class name.
You then noticed that and amended that issue like so:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
driver.execute_script("arguments[0].click();", buttonSearch)
However, that allowed the element to be located. But the webdriver is now unable to interact with the element. Because in the error it is explicitly stated that arguments[0].click is not a function.
So to fix this, instead of of using execute_script simply use click() instead. It's a simpler way, and the intended way. Like this:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD"))).click()
I'm doing automation testing using Selenium Python. So I tried to navigate back to previous page and I've include this line of code in my script:
driver.execute_script("window.history.go(-1)")
But then it is not working, it didn't navigate back to previous page. This is my whole code:
from selenium import webdriver
import time
#define variable driver
def func1():
driver = webdriver.Chrome("C:/Users/sabrina/Downloads/chromedriver_win32/chromedriver.exe")
driver.get("url")
return driver
driver = func1()
driver.maximize_window()
time.sleep(3)
#click email button
driver.implicitly_wait(10)
emailbutton = driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/button[1]')
emailbutton.click()
time.sleep(1)
driver.implicitly_wait(10)
#enter email & password
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/form/div[1]/div/div/div[1]/div/input').send_keys("email")
driver.implicitly_wait(50)
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/form/div[2]/div/div/div[1]/div/input').send_keys("password")
driver.implicitly_wait(50)
#assertion/checkpoint
element3 = driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[3]').text
assert element3 == "Forgot your password?"
#click Login with Email button
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[2]/button').click()
driver.implicitly_wait(50)
element4 = driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/div[1]/main/div[1]/div[1]/div[2]/button').text
assert element4 == "MAKE REQUEST"
#click Messages
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/header/div[1]/div/div/button[3]/div[2]').click()
time.sleep(2)
#choose requests with "Negotiation" status
element_negoreq = driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[2]/div/div/div/div/div/div[1]/div/div[3]/div[3]')
if (element_negoreq.text == "Negotiation"):
element_negoreq.click()
#click Book Now
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div/div[2]/div[1]/div/div/div/div[2]/div[2]/button[2]').click()
#tick Agree
time.sleep(3)
#driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[11]/div/div/div[1]').click()
#assert checkout word in current url
assert "checkout" in driver.current_url
print(driver.current_url)
#click Boost
driver.implicitly_wait(10)
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[12]/div/button[1]').click()
driver.execute_script("window.history.go(-1)")
All the codes before the navigate back script line are working until it reached the line. I also tried to use driver.back() but also not working. Is there anything to do with my indents or I don't include/import the related module? Anyone can advice?
To simply go back to the previous url do these two things.
driver.back()
driver.refresh()
As you have confirmed, after executing line
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[12]/div/button[1]').click()
A new page is loading. Since your page has not loaded completely and java scripts clicked back button, it was not effective. You can induce some sort of wait mechanism here to make sure page is loaded completely. Simplest thing would be to use time.sleep() but it’s not a good solution. Best solution I can think of is using explicit wait like below:
driver.find_element_by_xpath('//*[#id="q-app"]/div/div[2]/main/div/div[12]/div/button[1]').click()
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '<xpath of any element on new page>'))) #It will insure page is loaded completely.
driver.execute_script("window.history.go(-1)")
Please try above code, it will navigate back as expected.