Extracting a part of message from Yopmail using Python Selenium - python-3.x

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

Related

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

How to locate the element within an iframe using Selenium WebDriver through Python

While doing automation testing on this website http://www.scstrade.com/TechnicalAnalysis/tvchart/ i was unable to find the element using selenium.
i want to find the search bar element at the top which is used to search for stocks and then pass the name of desired stock in that bar using selenium.
Here is the xpath for the search bar :
/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input
here is my code:
from selenium import webdriver
driver = webdriver.Chrome("D:\PyCharm Projects\Web Automation\drivers\chromedriver.exe")
driver.get("http://www.scstrade.com/TechnicalAnalysis/tvchart/")
driver.find_elements_by_xpath('/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input')
Output : []
i also tried finding the element using the class name:
driver.find_element_by_class_name('input-3lfOzLDc-')
which gives me this error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"input-3lfOzLDc-"}
(Session info: chrome=83.0.4103.61)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
The element i am trying to access only has the class name so i can't try using the id.
I also tried switching to the frame first but i can't even find the frame element for this website using selenium.
The search bar element at the top left corner is within an <iframe> so to invoke send_keys() on the element 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, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='charting_library/static/en-tv-chart']")))
index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#header-toolbar-symbol-search input")))
index.click()
index.clear()
index.send_keys("KSE 30")
Using XPATH:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#src, 'charting_library/static/en-tv-chart')]")))
index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='header-toolbar-symbol-search']//input")))
index.click()
index.clear()
index.send_keys("KSE 30")
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 discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python

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 - Finding element based on ember

I am working with selenium in python 3.6 on the chrome browser. I have programmed it to the point where I can access the website I want but I am struggling to find the text box element I am searching for. When I inspect the element it has this code.
<input placeholder="" id="ember32" class="ssRegistrationField ssEmailTextboxField ember-text-field ember-view" type="email">
But when I try and use the given ID, it does not work and says that it cannot be found. Here is my code (Without the text I wish to insert of the website URL):
from selenium import webdriver
browser = webdriver.Chrome('chromedriver.exe')
browser.get('')
email = browser.find_element_by_id("ember34")
email.send_keys('')
I have just started using Selenium today and any help figuring out what is wrong would be very appreciated.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser,5).until(
EC.presence_of_element_located((By.ID,'ember32')))
browser.find_element(By.ID,'ember32').send_keys('Your_Email')
The problem was the DOM has ember32 and your program is looking for ember34, Basic Typeo.
The code above will add an implicate wait for 5 seconds, search for ember32, and then time out if it cant find it.
The desired element is an Ember.js element so to 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 CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ssRegistrationField.ssEmailTextboxField.ember-text-field.ember-view[id^='ember'][type='email']"))).send_keys("Max")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ssRegistrationField ssEmailTextboxField ember-text-field ember-view' and starts-with(#id,'ember')][#type='email']"))).send_keys("Max")
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 to click on the ember.js enabled button using Selenium and Python
Ember dropdown selenium xpath

How to check a checkbox in Chrome using Selenium with python

I'm trying to check a checkbox via selenium in Chrome using python3.
This is the HTML code:
<header class="list-header">
<aside class="list-header-bulk-selection">
<input type="checkbox" class="sc-cSHVUG iAwiCZ">
::after
I'm trying to check the box by:
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.click()
I am able to find the location, but unfortunately I get the following error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.142)
I think I have to access the ::after line, but I have no clue how I should do this.
Try following options to click on checkbox.
Option1:
location_once_scrolled_into_view
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.location_once_scrolled_into_view
check_mark.click()
Option2:
WebDriverWait and element_to_be_clickable
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
check_mark.click()
Option3:
Java Scripts Executor
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
driver.execute_script("arguments[0].click();", check_mark)
You need to import followings to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This sc-cSHVUG iAwiCZ looks suspicious and most probably it's automatically generated each time you refresh the page. I would recommend to stick to something more "static", for example the checkbox should have some label or other text explaining what it does and you could stick to that text.
From what you've provided so far it might be better to locate this <aside> tag first and then use child axis to reach the checkbox like:
//aside[contains(#class, 'bulk-selection')]/child::input
More information:
XPath Tutorial
XPath Axes
XPath Operators & Functions
The desired element is a dynamic element so to click() on 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, "header.list-header>aside.list-header-bulk-selection>input[type='checkbox']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//header[#class='list-header']/aside[#class='list-header-bulk-selection']/input[#type='checkbox' and #class]"))).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
Thank you guys so much. The thing was that ::after is a pseudo-element which is not interactable by selenium.
I actually solved it by accessing the checkbox and then hoovering over it (just a tiny bit) to sort of activate the HTML object. Then the checkbox was interactable.
from selenium.webdriver import ActionChains
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
ActionChains(driver).move_to_element_with_offset(check_mark,0.001,0.001).click().perform()

Resources