Selenium trying to get elements - python

I'd like to get the elements by Selenium as the attached pic:
ess-cell class="data-numeric"
However, the following code didn't work even though:
find_element_by_tag_name('div')
part works correctly. Does anyone know why?
row.find_element_by_tag_name('div').find_element_by_tag_name('div').find_elements_by_tag_name('ess-cell')

To get the elements <ess-cell class="data-numeric"...> you can use either of the following Locator Strategies:
Using css_selector:
elements = row.find_elements(By.CSS_SELECTOR, "div > div ess-cell.data-numeric")
Using xpath:
elements = row.find_elements(By.XPATH, "./div/div//ess-cell[contains(#class, 'data-numeric')]")
Ideally you have to induce WebDriverWait for presence_of_all_elements_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
elements = WebDriverWait(row, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div > div ess-cell.data-numeric")))
Using XPATH:
elements = WebDriverWait(row, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "./div/div//ess-cell[contains(#class, 'data-numeric')]")))
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

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 the first matching element having a specific background-url using Selenium Python

HTML:
<div class="__section-image" style="background-image: url("/img/all/05_element/continentImg/1.png");"></div>
The website have a lot of this html. I want to click the first of them.
To click on the first matching element you can use either of the following locator strategies:
Using css_selector:
driver.find_elements(By.CSS_SELECTOR, "div[class='__section-image']")[0].click()
Using xpath:
driver.find_elements(By.XPATH, "//div[#class='__section-image']")[0].click()
Ideally to click on the first matching element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[class='__section-image']")))[0].click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='__section-image']")))[0].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

No such element error but the element can be located in the DOM through xpath expression

I have a python script logging into our company website to then get each row of data. It logs me in, locates the iframe where the table with row values is located, from there I try to locate a row in the iframe table and attached is a picture:
Highlighting the correct xpath expression but I need all 31 values.
All my code works properly except this:
elem5 = browser.find_element_by_xpath("//td/label[contains(#id, 'driver')][1]")
find_element_by_* returns a single webelement, where as you are looking for all the 31 elements. So you need find_elements* and you can use either of the following Locator Strategies:
Using css_selector:
elements = driver.find_elements(By.CSS_SELECTOR, "td > label[id^='driver']")
Using xpath:
elements = driver.find_elements(By.XPATH, "//td/label[starts-with(#id, 'driver')]")
To find all the desired elements ideally you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "td > label[id^='driver']")))
Using XPATH:
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//td/label[starts-with(#id, 'driver')]")))
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

Python Selenium - find item in a list

I have the following:
Aliases
NAT
Rules
I can't seem to be able to locate the element with text Aliases. Is it possible to do?
Can anyone help me out?
You can use like:
//*[text()="Aliases"]
another way is
//a[#href="/firewall_aliases.php" and (text()="Aliases")]
You can use the following XPath locator:
//a[text()="Aliases"]
For more uniqueness you can use
//a[#href="/firewall_aliases.php" and (text()="Aliases")]
Or even
//a[#href="/firewall_aliases.php" and (#class="navlnk") and (text()="Aliases")]
To locate the element with text as Aliases you can use either of the following Locator Strategies:
Using link_text:
element = driver.find_element(By.LINK_TEXT, "Aliases")
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "a.navlnk[href*='firewall_aliases']")
Using xpath:
element = driver.find_element(By.XPATH, "//a[text()='Aliases' and contains(#href, 'firewall_aliases')]")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.LINK_TEXT, "Aliases")))
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.navlnk[href*='firewall_aliases']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[text()='Aliases' and contains(#href, 'firewall_aliases')]")))
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 to extract the text from the HTML using Selenium and Python

I have this HTML:
And I want get this text "rataoriginal". (This text changes, I need this part of the code as text)
I've tried
xpath = "//span[#class='_5h6Y_ _3Whw5 selectable-text invisible-space copyable-text']"
auxa = driver.find_element_by_xpath(xpath).text
print(auxa)
But it prints the same as print("\n"). I don't want to use beaultifulsoup for a while.
This HTML is from 'https://web.whatsapp.com'
The WebElement is a dynamic element, so to print the values you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.selectable-text.invisible-space.copyable-text[dir='auto']"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class, '') and contains(#class, 'invisible-space')][contains(#class, '') and #dir='auto']"))).text)
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
References
You can find a relevant discussion in:
How to retrieve the text of a WebElement using Selenium - Python
//*[contains(text(),"rataoriginal")] please use this xpath

Categories

Resources