Selenium can't find locate download link element by ID in Python - python

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

Related

Clicked button doesn't really function by Selenium Python

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

driver.execute_script("window.history.go(-1)") not working in chrome driver

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.

How to click buttons with Python Selenium webdriver on this website when the element cannot be found?

I'm very new to Selenium with Python and I'm simply trying to click 2 consecutive buttons on this website: https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28
I want to click the settings icon in the top right corner and then click the celsius option to convert temperatures on the site.
I can achieve this whilst debugging the code and doing the steps one at a time, but when I run the code normally, it is unable to find the second element with the error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="wuSettings-quick"]/div/a[2]"}
(Session info: chrome=85.0.4183.83)
I have tried identifying the elements by id and xpath as follows:
Button 1: driver.find_element_by_id('wuSettings').click()
Button 2: driver.find_element_by_xpath('//*[#id="wuSettings-quick"]/div/a[2]').click()
Hoping it's an easy fix. All help appreciated. Thanks.
Code below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
driver.implicitly_wait(15)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'wuSettings'))
)
element.click()
# element = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, '//*[#id="wuSettings-quick"]/div/a[2]'))
# )
# element.click()
except:
driver.quit()
Your code works fine if I replace this line:
driver.implicitly_wait(15)
with
time.sleep(5)
When I need to await for an element to be clickable, I usually have the best luck with something like this
can_click=False
while not can_click:
try:
element=driver.find_element_by_id('wuSettings')
element.click()
# note that if the issue is that the element is not visible in
# the viewport, you may need to do
# driver.execute_script("arguments[0].click();", element)
# instead
can_click=True
except:
pass
basically it should throw an exception until you can click on the element you want and once it does the loop terminates.
First of all you should not use both Implicit and explicit wait together. A cocktail of it could cause an unpredictable wait times. As you are using explicit wait for both buttons, it should be more than sufficient.
Also use of time.sleep might work in this case but its never been a good way to wait in test script as it makes test brittle and unpredictable.
As you can see even after page load few ajax components (Record for day passed in url is loading) are still loading and even though setting element is present its not giving option to change temp, unit record is fully loaded. So explicit / implicit wait on setting icon is not working.
Try below code and it should work.
try:
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//div[#class='legend-def temperature']"))) # To insure record for the day is loaded
ele = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, "wuSettings")))
driver.execute_script("arguments[0].scrollIntoView();", ele)
ele.click() #Scroll to Setting icon
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Switch to Metric']"))).click()
except:
driver.quit()
I tried different things, but it looks like you have to agree cookies use before you can click on another button.
I tried Matt's solution but I'm pretty sure that the commercial loading on top of the page screws up the first click on the settings button.
This is working fine for me :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
cookies = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//button[#id="truste-consent-button"][#type="button"]'))
)
cookies.click()
time.sleep(5)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//button[#id="wuSettings"][#class="wu-settings"]'))
)
element.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="wuSettings-quick"]/div/a[2]'))
)
element.click()

Why Cant I Click an Element in Selenium using python and Chrome Webdriver?

I use Selenium-python to scrape this page and click on Pagination numbers.
i try this one:
driver = webdriver.Chrome()
time.sleep(5)
driver.get(
'http://www.ooshop.com/courses-en-ligne/ContentNavigation.aspx?TO_NOEUD_IDMO=N000000013081&FROM_NOEUD_IDMO=N000000013056&TO_NOEUD_IDFO=81018&NOEUD_NIVEAU=2&UNIVERS_INDEX=2')
nbdespages = 23
for i in xrange(2,nbdespages):
time.sleep(10)
number = str(i)
if(i<10):
cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl0%s_lbPage' %number
else:
cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl%s_lbPage' %number
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, cssSelec)))
driver.execute_script("arguments[0].click()", element)
driver.find_element_by_css_selector(cssSelec).click()
finally:
driver.save_screenshot('/Users/Parik/Desktop/test.jpg')
print "TRY Again, you will find"
but i got this exception:
TimeoutException: Message:
i use WebDriverWait because sometimes i got this error if i don't use it and i read this question and use it for my problem:
Element is not clickable at point
the value of css selector is correct
POST http://127.0.0.1:51099/session/ddf78c5a19e720909cbe6a0f5408788e/element {"using": "css selector", "sessionId": "ddf78c5a19e720909cbe6a0f5408788e", "value": "ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl02_lbPage"}
UPADTE1
UPDATE 2
With Screen shot i see that i can't see the numbers, i changed my code to this:
try:
element = driver.find_element_by_css_selector('img#ctl00_cphC_pn3T1_ctl01_rp_ctl15_ctl00_iVisu.image')
driver.execute_script("return arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click()", element)
driver.find_element_by_css_selector(cssSelec).click()
finally:
driver.save_screenshot('/Users/Parik/Desktop/test.jpg')
print "TRY Again, you will find"
and now i see the Pagination
but i have always this exception:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"ctl00_cphC_pn3T1_ctl01_rptPaginationB_ctl02_lbPage"}
(Session info: chrome=51.0.2704.103)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.5 x86_64)
You should try .execute_script to perform click as below :-
try:driver.execute_script("document.getElementById(arguments[0]).click();", cssSelec)
You can also try as below :-
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, cssSelec))
)
driver.execute_script("arguments[0].click()", element)
Note :- you are preparing locator for id but using as By.CSS_SELECTOR to finding the element, for that reason you are getting exception as NoSuchElementException, so change it to By.ID.
You can also use element.click() but if you are getting Exception as like Element is not clickable at point, I suggest you can use here driver.execute_script("arguments[0].click()", element) to perform click.
Hope it will help you..:)
cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl0%s_lbPage' %number and
cssSelec = 'ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl%s_lbPage' %number refer to the id's for the elements you're trying to click and are not applicable CSS selectors by themselves - you can use them in a CSS selector using the # symbol like so:
a#ctl00_cphC_pn3T1_ctl01_rptPaginationH_ctl02_lbPage
Another method you could use to locate and click the elements is to use WebDriver's link text locator as the elements are anchor (<a> tag) elements.
I would highly recommend you avoid using Javascript to click the elements as this a hack at best and opens up potential for Javascript errors in your tests.

python selenium get asp-classic element

I tried find this URL page to do some selenium automation test, usually can get web element but some page is strange...i can not find the web element even using XPATH (use chrome browser can see the element, But can not do some action for this element..in fact...wrong message is "can not find the element)
looks like is asp.classic generate page....windows.onload() page
driver_path = os.path.join(os.getcwd(), "IEDriverServer.exe")
driver = webdriver.Ie(driver_path)
driver.get(mytestpage)
element = WebDriverWait(driver, 300).until(
EC.element_to_be_clickable((By.LINK_TEXT, "Hardware")))
element.click()
element = WebDriverWait(driver, 300).until(
EC.element_to_be_clickable((By.XPATH, "'//*[#id="testpage"]/table/tbody/tr/td[2]/table/tbody/tr[1]/td[2]/font/a[1]'")))
element.click()
Have you tried with selenium explicit wait? This might be because a page is getting loaded fully while you are performing selenium action on that page. Also, check response time for loading page.

Categories

Resources