click() doesn't work in selenium - python

i am currently using selenium with python and my webdriver is firefox
i tried the click event but it doesn't work
website = www.cloudsightapi.com/api
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
from selenium.webdriver.common.action_chains import ActionChains
import os
driver = webdriver.Firefox()
driver.get("http://cloudsightapi.com/api")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
element.click()
Please help !!

Clicking via javascript worked for me:
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
driver.execute_script("arguments[0].click();", element)
Now, the other problem is that clicking that element would only get you into more troubles. There will a file upload popup being opened. And, the problem is, you cannot control it via selenium.
A common way to approach the problem is to find the file input and set it's value to the absolute path to the file you want to upload, see:
How to upload file ( picture ) with selenium, python
In your case, the input is hidden, make it visible and send the path to it:
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input.dz-hidden-input[type=file]")))
# make the input visible
driver.execute_script("arguments[0].style = {};", element)
element.send_keys("/absolute/path/to/image.jpg")

Related

Can't locate a button using selenium to press on it

I've created a script in python using selenium to click on a like button available in a webpage. I've used xpath in order to locate that button and I think I've used it correctly. However, the script doesn't seem to find that button and as a results it throws TimeoutException error pointing at the very line containing the xpath.
As it is not possible to hit that like button without logging in, I expect the script to get the relevant html connected to that button so that I understand I could locate it correctly.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.instagram.com/p/CBi_eIuAwbG/"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,10)
driver.get(link)
item = wait.until(EC.visibility_of_element_located((By.XPATH,"//button[./svg[#aria-label='Like']]")))
print(item.get_attribute("innerHTML"))
How can I locate that like button visible as heart symbol using selenium?
To click on Like Button induce WebDriverWait() and wait for visibility_of_element_located() and below xpath.
Then scroll the element into view and click.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/p/CBi_eIuAwbG/")
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//button[.//*[name()='svg' and #aria-label='Like']]")))
element.location_once_scrolled_into_view
element.click()
You can do it like this
likeSVG = driver.find_element(By.CSS_SELECTOR, 'svg[aria-label="Like"]')
likeBtn = likeSVG.find_element(By.XPATH, './..')
likeBtn.click()
likeBtn is equal to the parent of the likeSVG div as you can use XPATH similar to file navigation commands in a CLI.
Try using the .find_element_by_xpath(xPath) method (Uses full xpath):
likeXPATH = "/html/body/div[1]/section/main/div/div[1]/article/div[2]/section[1]/span[1]/button"
likeElement = driver.find_element_by_xpath(likeXPATH)
likeElement.click()

Pressing ctrl+t doesn't work in Selenium Webdriver using ActionChains

I need to open a new browser tab in my test and I've read that the best approach is to simply send the appropriate keys to the browser. I'm using windows so I use ActionChains(driver).send_keys(Keys.CONTROL, "t").perform(), however, this does nothing.
I tried the following to test that Keys.CONTROL is working properly:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "v").perform()
This indeed passes whatever I have copied in the clipboard to the Google search box that is in focus by default.
This is what I want to use but that is not working:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()
Nothing seems to happen to the browser, no new tab opened, no dialog box, no notification. Does anyone know why this is?
Try this java Script Executor it should work.
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
Edited code with window handle.
driver=webdriver.Chrome()
driver.get("https://www.google.com")
window_before = driver.window_handles[0]
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
driver.find_element_by_name("q").send_keys("test")
try executing this script:
driver.execute_script("window.open('https://www.google.com');")
for example
myURL = 'https://www.google.com'
driver.execute_script("window.open('" + myURL + "');")
You’ve gotten some good answers utilizing JavaScript execution, but I am curious why your example doesn’t work in the first place.
It’s possible that your ActionChains line is executed before the page has fully loaded; you could try adding a wait as follows:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.TAG_NAME("body")))
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()

Python 2.7 Selenium No Such Element on Website

I'm trying to do some webscraping from a betting website:
As part of the process, I have to click on the different buttons under the "Favourites" section on the left side to select different competitions.
Let's take the ENG Premier League button as example. I identified the button as:
(source: 666kb.com)
The XPath is: //*[#id="SportMenuF"]/div[3] and the ID is 91.
My code for clicking on the button is as follows:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL Removed")
content = driver.find_element_by_xpath('//*[#id="SportMenuF"]/div[3]')
content.click()
Unfortunately, I always get this error message when I run the script:
"no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="SportMenuF"]/div[3]"}"
I have tried different identifiers such as CCS Selector, ID and, as shown in the example above, the Xpath. I tried using waits and explicit conditions, too. None of this has worked.
I also attempted scraping some values from the website without any success:
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL removed")
content = driver.find_elements_by_class_name('price-val')
for entry in content:
print entry.text
Same problem, nothing shows up.
The website embeddes an iframe from a different website. Could this be the cause of my problems? I tried scraping directly from the iframe URL, too, which didn't work, either.
I would appreciate any suggestions.
Sometimes elements are either hiding behind an iframe, or they haven't loaded yet
For the iframe check, try:
driver.switch_to.frame(0)
For the wait check, try:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '-put the x-path here-')))

why is this code showing NoSuchElementException error? I checked Chrome DOM my XPATH able to find the destinated tag

why is this code showing NoSuchElementException error? I checked Chrome DOM my XPATH able to find the destinated tag.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
class Firefox():
def test(self):
base_url='https://oakliquorcabinet.com/'
driver = webdriver.Chrome(executable_path=r'C:\Users\Vicky\Downloads\chromedriver')
driver.get(base_url)
search=driver.find_element(By.XPATH,'//div[#class="box-footer"]/button[2]')
search.click()
ff=Firefox()
ff.test()
Selenium by default waits for the DOM to load and tries to find the element. But, the confirmation pop up becomes visible after some time the main page is loaded.
Use explicit wait to fix this issue.
add these imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
change line in script:
search = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//div[#class="box-footer"]/button[2]')))

Unable to submit keys using selenium with python

Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working code:
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
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)

Categories