find_element selenium code example
Example 1: selenium finding elements
from selenium import webdriver
chrome_driver_path = 'C:\Development\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get('https://www.python.org')
name = driver.find_element_by_name('q')
print(name.get_attribute('placeholder'))
python_logo = driver.find_element_by_class_name('python-logo')
print(python_logo.size)
documentation_link = driver.find_element_by_css_selector('.documentation-widget a')
print(documentation_link.text)
anchor_xpath = driver.find_element_by_xpath('//*[@id="search"]/div[2]/div[6]/div[1]/div/div'))
print(anchor_xpath.text)
driver.quit()
Example 2: find elements in selenium
h1 = driver.find_element_by_name('h1')
h1 = driver.find_element_by_class_name('someclass')
h1 = driver.find_element_by_xpath('//h1')
h1 = driver.find_element_by_id('greatID')
Example 3: how to find selenium webelement java
//required imports
WebDriver driver = new ChromeDriver();
WebElement objWE;
//There are many way most common are id and xpath
objWE = driver.findElement(By.id("String of id attribute"));
//To find the xpath easily hit F12 and right click the element you want
//and copy xpath then paste it
objWE = driver.findElement(By.xpath("//*[@id="search"]/div[2]/div[6]/div[1]/div/div"));
Example 4: findelements in selenium
List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));
Example 5: selenium ways of finding
elementcss= driver.findElement(By.cssSelector('div.nav-search-input'))