Unable to extract attribute value with selenium - python-3.x

caller = driver.find_element_by_id("sys_display.new_call.caller")
print(caller.get_attribute('value'))
Hello running into issues when trying to extract a attribute value from a element.
I have tried using caller= driver.find_element_by_id("sys_display.new_call.caller").get_attribute('value')
but it doesnt seem to pull the value at all from HTML.
I am a noobie thanks for the help!

To print the value of the value attribute i.e. MyMercy User you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element_by_css_selector("input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']").get_attribute("value"))
Using xpath:
print(driver.find_element_by_xpath("//input[#id='sys_display.new_call.caller' and #data-name='caller']").get_attribute("value"))
Ideally you need 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, "input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']"))).get_attribute("value"))
Using XPATH:
driver.get('https://www.temporary-mail.net/')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#id='sys_display.new_call.caller' and #data-name='caller']"))).get_attribute("value"))
Console Output:
MyMercy User
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

In selenium if the value is stored as innerText, you have to retrive it using .text method , this works only if the element is visible in the UI. Means if its hidden or out of view port .text method will return blank. In that case you could use get attribute "textContent" . But for testing textCOntent is not recommended as it won't validate if the text is displaye in UI. It is recommended only for web scraping
You can also wait for value field to have the text before printing you are indeed looking for value attribute
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
caller = wait.until(EC.text_to_be_present_in_element_value((By.ID, 'sys_display.new_call.caller'),"MyMercy User"))
caller = driver.find_element_by_id("sys_display.new_call.caller")
print(caller.get_attribute('value'))
print(caller.text)
print(caller.get_attribute('textContent'))
Try printing the three values and see which one gives the result you want

Related

Extracting a part of message from Yopmail using Python Selenium

I want to Extract a Specific part in Python Selenium. I have done it with Pyautogui but I want to do it without that is it possible?
https://yopmail.com/
put in inbox tab
jenniferwilks09182
I want to extract exactly this code it is in a separate div
The element with the code is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be visible.
You can use either of the following Locator Strategies:
Using XPATH and following:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='ifmail']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[.='Your code is:']//following::div[1]"))).text)
Using XPATH and following-sibling:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='ifmail']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[.='Your code is:']//following-sibling::div[1]"))).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
Reference
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Python/Selenium Access element inside of iframe

I'm trying to access some elements inside of iframe but withour success. Basically, I found the iframe, switch to it, but I can get the element inside of it. All elements that I try, I always get a message saying that the element was not found.
Could you please help?
HTML:
You can use the below code to switch to frame and then interact with the desired element :-
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(#sandbox,'allow-popups-to-escape-sandbox')]")))
ele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.analytics-ui-application")))
#ele is a web element so you can trigger .click, .text or any other web element methods on it.
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
As the elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
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,"frame-router[activeclient='analyticsUi'] > iframe[sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "css_clickable_element"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame-router[#activeclient='analyticsUi']/iframe[#sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath_clickable_element"))).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:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Cant Find Element in Selenium Python

Hello I am trying to use selenium to find a button to click on. Below is a snippet of the HTML code i am working with.
<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
I am trying to click on the runButton with the code below.
elem = driver.find_element_by_id('runButton').click()
I am getting the following error message:
NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
Not sure what else to try.
Most likely what you'll need to do to find your element is to use waits. You need to allow time for an element to be visible, clickable, etc. before you can interact with it. You can find information on waits here: https://selenium-python.readthedocs.io/waits.html
Taken from the above website:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
If waits do not work then it's possible that your element is inside an iframe. You will need to switch to that iframe first and then search for the element in order to find it.
You will find the iframe like you would another element and then switch to it like this:
iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)
button = driver.find_element_by_id("runButton")
button.click()
Once you're done with the iframe and it's contents, you will need to switch back out of it:
driver.switch_to.default_content()
The element seems to be a dynamic element so to click() on the element you need to use 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, "input.button#runButton[value='Run Report']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #id='runButton'][#value='Run Report']"))).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.common.exceptions.ElementNotInteractableException: Message: element not interactable sending text to First name field using Selenium Python

Through Selenium, I try to enter the username on one popular site, but, for some reason I do not understand, this does not work. Everything opens, a click occurs on the desired input field, but immediately after that an error appears when trying to enter text.
Here is my code:
webdriverDir = "./chromedriver.exe"
home_url = 'https://appleid.apple.com/account/'
browser = webdriver.Chrome(executable_path=webdriverDir)
browser.get(home_url)
elem = browser.find_element_by_id("firstNameInput")
elem.click()
elem.send_keys('namename')
time.sleep(5)
I also tried to change the solution to this:
browser.find_element_by_id("firstNameInput").send_keys("namename")
But it also does not work. I can’t understand what’s the matter.
Also tried through xpath, class_name and css_selector. The result was either the same or the element was simply not detected. Although everything was copied from element code from console.
Error Code:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Where could there be a mistake? Or can it be the site itself?
The element with firstNameInput id is not an input and it's a custom tag and your send-keys commands can not access that element.
what you have to do is, access the input present in the parent element.
driver.find_element_by_css_selector("#firstNameInput input").send_keys('hello')
To send a character sequence to the First name field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://appleid.apple.com/account/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "first-name-input#firstNameInput input"))).send_keys("haruhi")
Using XPATH:
driver.get("https://appleid.apple.com/account/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='form-label' and normalize-space()='First name']//preceding::input[1]"))).send_keys("haruhi")
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
Browser Snapshot:

Python Selenium click checkbox without id and name

I'm trying to set up an automation that can help me work easier. I want to log in to a device automatically and set up some initial settings. The part I am having trouble with is that I have to check a checkbox to continue, but I couldn't find a way to click on the button.
This is the XPath for that button:
//*[#id="tableHdd"]/div/div[1]/span[1]/input
broswer.find_element_by_class_name('table-cell').find_elements_by_class_name('checkbox').click()
To clcick() on the checkbox you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#tableHdd > div.table > div.table-header > span.table-cell > input.checkbox"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='tableHdd']/div[#class='table']/div[#class='table-header']/span[#class='table-cell']/input[#class='checkbox']"))).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
You're misusing Element Locators
find_element_* should return a single unique WebElement. If the locator matches > 1 element - the first match will be returned.
find_elements_* - returns the List of WebElements, you cannot invoke click() function from the list
So if there is only one checkbox at the page - you can tick it like:
driver.find_element_by_class_name("checkbox").click()
If there are more checkboxes and you need to tick particular that one - use slightly amended XPath expression:
driver.find_element_by_xpath("//div[#id='tableHdd']/descendant::*/span[#class='table-cell']/input").click()

Resources