How to click on the download button using Selenium and Python - python

I am trying to download an excel file using Selenium Python using the below code:
download=driver.find_element_by_css_selector("span[data-reactid='.0.1.0.0.1.1.1.0.1.0.1'>Download Orders]")
The element of the download button look like:
The piece of code above isnt working. Can someone help?

Your CSS locator is wrong. Please try to click with below xpath.
//span[.='Download Orders']

The desired element is a React element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH with normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Download Orders']"))).click()
Using XPATHand contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Download Orders')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

I agree with Muzzamil that the CSS locator you have used seems wrong. # is needed to select the attribute data-reactid.
You can try XPath:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1']
If you want to include also the text, try:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1'][contains(text(),'Download Orders')]

Related

Selenium how to capture element screenshot?

I am trying to access the "canvas" element, sin the screenshot.
I am running the following code:
driver.find_element_by_tag_name('canvas').screenshot("canvas.png")
driver.find_element_by_tag_name('canvas').screenshot_as_png("chess.png")
Neither of the 2 above work, what am I missing?
You were close enough. The <canvas> tag is within the <chess-board> tag.
Solution
Ideally you need to induce WebDriverWait for the visibility_of_element_located() for the <canvas> element and you can use either of the following locator strategies:
Using TAG_NAME:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.TAG_NAME, "canvas"))).screenshot_as_png("chess.png")
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "chess-board > canvas"))).screenshot_as_png("chess.png")
Using XPATH:
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//chess-board/canvas"))).screenshot_as_png("chess.png")
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
1)Create a class. Implement TestNG 'ITestListener'.
2)Call the method 'onTestFailure'.
3)Add the code to take a screenshot with this method.
4)Get the Test method name and take a screenshot with the test name. Then place it in the desired destination folder.

How to click on td element using selenium python

So I'm Fairley new to selenium and I'm attempting click on a table but I can't seem to find it.
Below I have posted my code along with a screen shot of what I'm trying to click on.
Here is my code:
competitorPrices = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.LINK_TEXT, "Competitor Prices"))).click()
HTML snapshot:
Error:
Element snapshot:
I would try something like this:
...
import time
time.sleep(5)
xpath = "//*[contains(text(),'Competitor Prices')]"
element = driver.find_element(by=By.XPATH, value=xpath)
competitorPrices = driver.execute_script("$(arguments[0]).click();", element)
Sometimes the method element_to_be_clickable doesn't work out of the box. In this cases to debug I use time.sleep so I can understand better the problem
The desired element is a <td> not an <a> tag. Hence LINK_TEXT won't work.
Moreover click() doesn't returns anything, so competitorPrices will be always None which you may remove.
Solution
You can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "table#tblTabStrip td[ch^='CompetitorPrices']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[#id='tblTabStrip']//td[text()='Competitor Prices']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How can I make Selenium click on a href by css selector?

I have been trying to make a Selenium bot that searches for a word on github, clicks on the first link and then downloads it with Python 3.8 and I got stuck with making the bot click on the hyperlink. I understand that I can make the bot click on it with driver.find_element(By.XPATH, "Xpath").click() but I want to be able to find the path of the href with another method for the sake of learning, in this case CSS_SELECTOR. Source code of the first hyperlink result is like this:
HTML:
Since every single result is under the same "a" selector with a class of "v-align-middle", I thought of using this code: driver.find_element(By.CSS_SELECTOR, ".v-align-middle").click() but it did not seem to work. What am I missing?
The desired element is a dynamic element, so to click() on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.v-align-middle[href='/asdf-vm/asdf'][data-hydro-click][data-hydro-click-hmac]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='v-align-middle' and #href='/asdf-vm/asdf'][#data-hydro-click and #data-hydro-click-hmac]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Selenium python - can't find the right path to the button

I can't seem to find the right path for the button marked above. Any help on how to simulate a click on this button?
This is the website, and linked under is a greater part of the source code.
To click on the element with text as Lukk you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytcp-button#close-button div[label='Lukk']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytcp-button[#id='close-button']//div[#label='Lukk' and text()='Lukk']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I would say to first wait for the popup to open. Explicit wait can do:
Here is a sample code:
https://seleniumbyexamples.github.io/waitvisibility
Your selector would be paper-dialog#dialog
Once the popup is visible you can now wait for the button.
You can use wait to be clickable:
https://seleniumbyexamples.github.io/waitclickable
The id would be close-button
You can also try other locators:
https://seleniumbyexamples.github.io/locator

Click button by text using Python and Selenium

Is it possible to click multiply buttons with the same text with Selenium?
You can find all buttons by text and then execute click() method for each button in a for loop.
Using this SO answer it would be something like this:
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
for btn in buttons:
btn.click()
I also recommend you take a look at Splinter which is a nice wrapper for Selenium.
Splinter is an abstraction layer on top of existing browser automation
tools such as Selenium, PhantomJS and zope.testbrowser. It has a
high-level API that makes it easy to write automated tests of web
applications.
I had the following in html:
driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
To locate and click a <button> element by it's text you can use either of the following Locator Strategies:
Using xpath and text():
driver.find_element_by_xpath("//button[text()='button_text']").click()
Using xpath and contains():
driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
Ideally, to locate and click a <button> element by it's text you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH and text():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update
To locate all the <button> elements by the text you can use either of the following Locator Strategies:
Using xpath and text():
for button in driver.find_elements_by_xpath("//button[text()='button_text']"):
button.click()
Using xpath and contains():
for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"):
button.click()
Ideally, to locate all the <button> elements by the text you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using XPATH and text():
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))):
button.click()
Using XPATH and contains():
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))):
button.click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#nobodyskiddy, Try to use driver.find_element ( if you have a single button option ), use index to click() when you are using driver.find_elements, find_elements will return array to web element values, so you have to use the index to select or click.

Categories

Resources