How to find the element using selenium in python - python

I can't find the element of the html below.
<span class="tabComboBoxName" id="tab-ui-id-1565209097494" aria-hidden="true">20/07/2019</span>
I've tried the following codes:
elem = browser.find_elements_by_xpath("//class[#id='tab-ui-id-1565209097494']")
elem = browser.find_elements_by_class_name('tabComboBoxName')
elem = browser.find_elements_by_id('tab-ui-id-1565209097494')
For those tries I got an empty list.

The element is a dynamically generated element so to locate the element you need to induce WebDriverWait for the desired visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.tabComboBoxName[id^='tab-ui-id-']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='tabComboBoxName' and starts-with(#id, 'tab-ui-id-')][contains(., '20/07/2019')]")))
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

Make sure that the element doesn't belong to an <iframe>, if it does - you will need to switch_to() the iframe where the element lives prior to attempting to find it
Make sure that the element doesn't belong to ShadowDOM, if it does - you will need to locate the relevant ShadowRoot element, cast it to the WebElement and use find_element() function of the WebElement instead of driver
Make sure to use Explicit Wait as it might be the case the element is not immediately available and it's being loaded later via AJAX request
Try using another locator strategy, for instance you can stick to the element text like:
//span[text()='20/07/2019']

Related

How to locate an element inside this iframe with selenium?

I am tring to access an element inside this iframe:
I tried to use switch_to.frame(0) first, but still can not locate the element inside the frame.
Screenshot of the error:
As the element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='To Do Assignments in Connect'][src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='To Do Assignments in Connect' and #src='https://connect.mheducation.com/paamweb/index.html#/access/home']")))
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 couple of relevant detailed discussions in:
How can I select a html element no matter what frame it is in in selenium?
You should switch to the frame:
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

How can I find element by text but it's number in selenium

I want to get an element by text, but it is number? I guess?
For example,
<a class="ui-state-default" href="#">11</a>
It contain a number so this code didnt work
driver.find_element_by_xpath('//*[#text="11"]')
How can I find this element?
You can access a web element(i.e div tag in this case) by its text content this way:
driver.find_elements_by_xpath("//*[contains(text(), '11')]")
driver.find_element_by_xpath('//*[contains(text,"11")]')
Please use above xpath to workout with this element
To locate the element by it's text you can use either of the following xpath based Locator Strategies:
Using xpath and text():
element = driver.find_element_by_xpath("//*[text()='11']")
Using xpath and contains():
element = driver.find_element_by_xpath("//*[contains(., '11')]")
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 XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='ui-state-default' and text()='11']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='ui-state-default' and contains(., '11')]")))
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 click on the span element using Selenium and Python

I'm trying to identify the following element but no matter the method it doesn't see it.
<span onclick="onClickTab('details'); return false;" id = "details" name = "details" class style ="display: inline;"">...</span>
I've tried with: Xpath, relative xpath, onclick, onclick contains, by id, by name, just nothing works.
It is a clickable button which appears after selecting an item in a list.
Current code is:
try:
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[#onclick='onClickTab('details'); return false;']")))
except
print("Error")
driver.find_element_by_xpath("//span[#onclick='onClickTab('details'); return false;']").click()
if there are any minor syntax problems like a "(" or such it might be because I typed it by hand, that shouldn't be the issue.
I'm forever grateful if you could point me to the right direction.
To click on the <span> element instead of presence_of_element_located() you have 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, "span#details[name='details'][onclick^='onClickTab']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='details' and #name='details'][starts-with(#onclick, 'onClickTab')]"))).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 was able to select the element using the following xPath:
//span[contains(#onclick, "onClickTab(\'details\'); return false;")]
Using selenium, I used:
try:
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH,'//span[#onclick="onClickTab(\'details\'); return false;"]')))
except:
print("Error")
driver.find_element_by_xpath('//span[#onclick="onClickTab(\'details\'); return false;"]').click()
Here i suggest you to use id in Xpath.
driver.find_element_by_xpath("//span[contains(#id,'details')]").click()
if multiple element is there then you have to use foreach loop of driver.find_elements_by_xpath("//span[contains(#id,'details')]") and check with text and click on match element.

How to locate the button element using Selenium through Python

I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.
HTML:
To locate and click() on the element with text as Not Now you can use the following Locator Strategy:
Using xpath:
driver.find_element_by_xpath("//button[text()='Not Now']").click()
However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).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
Reference
You can find a couple of relevant discussions in:
What does contains(., 'some text') refers to within xpath used in Selenium
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text

Python Selenium: can't figure out xpath to clickable link element in span tag

I'm using selenium python and have tried more than a dozen ways to find a clickable link element from a span tag but have been unsuccessful. I've tried using xpath, by link text, partial link text, and other suggestions researched.
The last 3 attempts using:
browser.find_element_by_link_text("Web Time Clock-Eastern").click()
element = browser.find_element_by_partial_link_text('Clock-Eastern')
browser.wait.until(EC.element_to_be_clickable((By.XPATH, '//a[#span]/html/body/div[6]/div[1]/div[1]/div[1]/div/div/div[4]/div[2]button'))).click()
I've provided image of the inspected element html below:
I expect to locate an element I can pass click method to open corresponding web page.
To click() the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.cardLinks a[href*='php/timeclock/WEB'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='cardLinks']//a[contains(#href, 'php/timeclock/WEB')]/span[text()='Web Time Clock-Eastern']"))).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
Try the below xpath.
//span[normalize-space(.)='Web Time Clock-Eastern']/parent::a
One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate.
browser.find_element_by_xpath("//div[#class='cardLinks']/div/a").click()

Categories