Selenium how to capture element screenshot? - python

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.

Related

Find Text By Nth Class Name is Selenium

I would like to get the the Date time text on this webpage but had issues locating it. A help will be appreciated.
If I use this codde,
var = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'col-md-3'))).text
I get the text in the first class (col-md-39). How do a I get the text in third class in this instance.
The Date Time text is within a text node. So to print the text you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using XPATH and childNodes[n]:
print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='row']//div[#class='col-md-3'][.//strong[text()='Last communication']]")))).strip())
Using XPATH and splitlines():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='row']//div[#class='col-md-3'][.//strong[text()='Last communication']]"))).get_attribute("innerHTML").splitlines()[-1])
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
You need to change the locator to get the desired result:
By.XPath, '//*[text()="Last communication"]//parent::div'

TimeoutException while extracting the value of the title attribute using Selenium Python

I am trying to get the following count of an Instagram account. I believe that the XPATH is correct an exists. Here's a screenshot showing it exists when I search for it:
This is my code:
wait = WebDriverWait(driver, 30)
followers = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[1]/section/main/div/ul/li[2]/button/div/span")))
print(followers.get_attribute("title"))
I have even looked at similar projects that find the following count and our code is almost exactly the same.
The desired element is a dynamic element, so to locate the element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategy:
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(., 'followers')]//span[#class and #title and text()]"))).get_attribute("title"))
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

How can I select the image link from src html part using python selenium

I'm trying to get all the links of the images from the below mentioned html code type:
<img src="https://img.xyz.com/rcmshopping/PROD_IMAGES/fffff_2018_05_10_10_49_23.jpg">
I am not able to fetch any link from this part of html out f 3 any 1 is good for me. Im new to python please guide.
To print the value of the src attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='elevatezoom-gallery']/img"))).get_attribute("src"))
Using CSS_SELECTOR:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.elevatezoom-gallery>img"))).get_attribute("src"))
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
You can achieve that by using the find_elements_by_css_selector() function.
links = self.webdriver.find_elements_by_css_selector('img[src="https://img.xyz.com/rcmshopping/PROD_IMAGES/fffff_2018_05_10_10_49_23.jpg"])

How to click on the download button using Selenium and 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')]

Categories

Resources