Selenium: Can't locate element - python

I am trying to switch to frame but Selenium can't locate the element.
Here is HTML code which I get with Selenium before locating:
<div class="jqmPopOverlay" style="height: 100%; width: 100%; position: fixed; left: 0px; top: 0px; z-index: 2999; opacity: 0.7;"></div><div class="login login-main jqmID1" id="loginForm" style="z-index: 3000; top: 0px; left: 319.5px; display: block;">
<div class="titlebar">
<a class="close" href="#" tabindex="7">✕</a>
</div>
<div class="tooltipstered" id="loginFrame"><iframe frameborder="0" id="easyXDM_default127_provider" name="easyXDM_default127_provider" scrolling="no" style="height: 509.667px;" width="100%"></iframe></div>
</div>
<script async="" defer="" type="text/javascript">undefined</script><script async="" src="//www.googleadservices.com/pagead/conversion_async.js" type="text/javascript"></script><script async="" src="https://www.google-analytics.com/plugins/ua/ec.js" type="text/javascript"></script><script async="" src="https://www.google-analytics.com/analytics.js" type="text/javascript"></script><script async="" src="//www.googletagmanager.com/gtm.js?id=GTM-4DBM"></script><script type="text/javascript">
Here is Python code:
browser = webdriver.Chrome('chromedriver.exe')
browser.get(url)
frame = browser.find_element_by_id("easyXDM_default6255_provider")
browser.switch_to_frame(frame)
Here is the error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"easyXDM_default6255_provider"}
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)
As you can see there is the element in HTML, but it can't be located.
What am I doing wrong?

Try this:
browser = webdriver.Chrome('chromedriver.exe')
browser.get(url)
WebDriverWait(browser, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[#id = 'loginFrame']/iframe")))
Note: you have to add some imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
Firstly, your frame probably has generic id and name. That's why I have proposed another selector. Secondly I have added WebDriverWait, which waits at least 10 seconds until frame will be available and only then switches to it.
PS: when you are done with content inside iframe, don't forget to switch to default content like this:
browser.switch_to.default_content()
otherwise you will be not able to interact with the whole DOM.

Related

activating a HIDDEN menu on DIV tag with a RIGHT MOUSE CLICK using PYTHON SELENIUM

I am using Selenium on Python. I was unable to click on a Hidden Menu which seems to activate (make the web element visible) only using a mouse Right Click.
Upon Right Click on a web page icon this element appears on the Elements Inspector:
<div role="presentation" class="x-css-shadow" id="ext-element-16" style="box-shadow: rgb(136, 136, 136) 0px 0px 6px; z-index: 19000; left: 1163px; top: 596px; width: 162px; height: 218px;"></div>
Then underneath it appears also the List of Elements I need to click upon:
<div class="x-menu rptapp-notransparent x-layer x-menu-default x-border-box" style="z-index: 19000; height: 222px; left: 1163px; top: 592px; width: 162px;" role="menu" aria-hidden="false" aria-disabled="false" aria-expanded="true" id="menu-1899" tabindex="-1" data-componentid="menu-1899"><div id="menu-1899-bodyWrap" data-ref="bodyWrap" class="x-menu-bodyWrap" role="presentation"><div id="menu-1899-body" data-ref="body" class="x-menu-body x-menu-body x-unselectable x-menu-body-default x-box-layout-ct x-menu-body-default" role="presentation" style="left: 0px; top: 0px; width: 160px; height: 220px;"><div role="presentation" class="x-menu-icon-separator x-menu-icon-separator-default" id="ext-element-17"> </div><div role="presentation" id="menu-1899-before-scroller" class="x-box-scroller x-box-scroller-top x-box-scroller-menu x-box-scroller-menu-default x-unselectable" style="display:none"></div><div id="menu-1899-innerCt" data-ref="innerCt" role="presentation" class="x-box-inner x-box-scroller-body-vertical x-scroller" style="overflow: hidden; height: 216px; width: 156px;"><div id="menu-1899-targetEl" data-ref="targetEl" class="x-box-target" role="presentation" style="width: 156px;">
I tried many different way including this one:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
RunButton = '//*[#id="menu-1899"]'
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH, RunButton))).click()`
I want to make the menu visible so I can click on items from it.
Step1: this is initial view of the elements on the page:
enter image description here
Step2: the I right click on the report element I want to run:
enter image description here
Step3: This emenu element appears on the inspector and brings with it the subsequent elements I need to click upon on the Menu
enter image description here

How to click on the list item using selenium driver in Python3

I have the following HTML code
<ul id="vmStateDropDown" class="dropdown-content active" style="white-space: nowrap; position: absolute; top: 83px; left: 264px; opacity: 1; display: block;"><li class="waves-effect waves-default" style="display: block;"><a class="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">directions_run</i><span>Turn On</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">power_settings_new</i><span>Turn Off</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">loop</i><span>Restart</span></a></li></ul>
I need to click in the list item Turn On. Here is the code I have so far.
turn_vmon = driver.find_elements(By.XPATH,'/html/body/div[4]/main/div/div[3]/div[3]/div[1]/ul/li[1]')
When I print the text for turn_vmon variable, I can see the option which I need. However I am unable to figure out how to click on the list item.
Can someone please help?
To click on the element with text as Turn On you can use either of the following Locator Strategy:
Using xpath:
driver.find_element(By.XPATH, "//span[text()='Turn On']").click()
Ideally to click on the element 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, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Turn On']"))).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 Python: Sending keys to iframe

I'm currently a student trying to learn how to make a bot. I've been successful by myself traversing through a website until I need to send keys to an iframe. I tried the following to get a NoSuchElementException.
#imports from the beginning
from selenium import webdriver as wd
from selenium.common.exceptions import NoSuchElementException
import chromedriver_binary
iframe = wd.find_element_by_xpath('//*[#id="card-number-container"]/iframe')
wd.switch_to.frame(iframe)
num = wd.find_element_by_xpath('//*[#id="card-number-container"]')
type(num)
num.send_keys("867-5309")
I tried switching to the iframe to interact with it. The error starts when defining num, but I have no idea what else to put to get there. Here's some of the HTML from the website.
<div id="card-number-container" class="_3lT_PetJ2w_rU8njR_SoIK _1ysVkT9CIcv98OnRX5h1Rj flex-microform">
<iframe hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" src="https://flex.cybersource.com/cybersource/assets/microform/0.11.4/iframe.html?keyId=03eSdEAPS81iBTM0NCSfE2l3z43E8yw1#{"microformId":"cca792b9-2c83-422c-875b-bdb03b080eee","fieldId":"914ffbab-2597-40e3-9cd0-b5098cea829f","jwt":"eyJraWQiOiJ3ZiIsImFsZyI6IlJTMjU2In0.eyJmbHgiOnsicGF0aCI6Ii9mbGV4L3YyL3Rva2VucyIsImRhdGEiOiI5SGorT1h6Z3lWc2xIc0FxVjE3bEJSQUFFQjJZU1VaQzJjY0dLK0xFWndOVEtMRXhkTGVLS3NSZFRVQjZEODdGWEFTcHM2a3RPTWIwbUNiWTBDZUlOYVE5eTVJSEd4TVgzb2Ziby9keGZCZGVvaFhpRHl1V2tlQXRpZlFneXpXR21KaDUiLCJvcmlnaW4iOiJodHRwczovL2ZsZXguY3liZXJzb3VyY2UuY29tIiwiandrIjp7Imt0eSI6IlJTQSIsImUiOiJBUUFCIiwidXNlIjoiZW5jIiwibiI6ImlHbEJXdmlVVmlzTHpvZ0d1S0VNSDdjNVNtQVQxenYybXh0bExNU1k2a05XaUNQWW1RYW5BS2xRemp5MjN1NzlROG5SZk56aW50a3JWR1hhYk8ycndOQTRaYTBTdXp0ODVhU2steDdNSU9PSDVrQkRpeEJGWmc0dXQ2RDhZRHhHUW5uR25BZEZGZHF5Umx4VnhiZU53WUFxOHpxZFJ1UG9VckY0ZDYxZGFwejJ6ZFU1Z2QyZWFLZXEwQTdCTE9CdjhTbThLU1R6bDY3VndKNmJvX0VLM3FsUldBd3NiLXRSMFIzV0xZTFBDb2lVcThMeUNkVWpjSG84ckQtaGw1VzZ3ekkxR0x3S21RRVN3d0ZLdmZDSmwzTWs1TFQ0NU1WUmtHX1ViNlFBMkZwaDNKOXhGNkY2clNVM2Y0WnVFNlpEQnlIeXZGekVpdlJYMURjWVFxblpuUSIsImtpZCI6IjAzZVNkRUFQUzgxaUJUTTBOQ1NmRTJsM3o0M0U4eXcxIn19LCJjdHgiOlt7ImRhdGEiOnsidGFyZ2V0T3JpZ2lucyI6WyJodHRwczovL3d3dy5wb2tlbW9uY2VudGVyLmNvbSIsImh0dHBzOi8vdGVzdC5wb2tlbW9uY2VudGVyLmNvbSIsImh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCJdLCJtZk9yaWdpbiI6Imh0dHBzOi8vZmxleC5jeWJlcnNvdXJjZS5jb20ifSwidHlwZSI6Im1mLTAuMTEuMCJ9XSwiaXNzIjoiRmxleCBBUEkiLCJleHAiOjE2MzQyNDg5OTIsImlhdCI6MTYzNDI0ODA5MiwianRpIjoiYWF6VWc3QThadXJXZG8yZyJ9.BsUePIvHdd5gkjdKMQ2iaBxyiakGWK9fe0bZNdaCheKfThTqG2HvsipskZ6ec42mira64uEFoCR43qGT7NIoGnGy-CmZ5pYWeuM8yzULD6pSPYhN42afQN08Mht9KjeKSL7W-NlTqT_a3KPadYsm2PIAACDj3hRhVWf0A0Avkd88EN0Yuch47KiVBVJb4QtVONcIVum0rIvr5msnj2zaJyC6WvHhiUu8UutOHqeoqHT3pxdcQNoW6xai6yQjK38yUAVcfKhZpcSnlC-be5FYK38eYSjlAp0XgFSAi6_nllsAs8oj9OoB-A57HpBRyNS0QFVLSt3bmoQky2HMr8gGGg","microformConfig":{"styles":{"input":{"font-size":"12px"},":disabled":{"cursor":"not-allowed"},"valid":{"color":"#43a047"},"invalid":{"color":"#d32f2f"}}},"config":{"placeholder":"Card Number..."},"fieldType":"number"}" style="overflow: hidden; position: relative; border: none; width: 100%; height: 100%;" cd_frame_id_="e0aaadb108c0aacce0bb0c878467c1c7"></iframe>
</div>

Retrieve url with Selenium

I want to retrieve with Selenium a full url, using part of it.
<div class="productcarousel__mainitem slick-slide slick-current slick-active" data-slick-index="0" aria-hidden="false" style="width: 280px; position: relative; left: 0px; top: 0px; z-index: 999; opacity: 1;" tabindex="-1" role="option" aria-describedby="slick-slide00">
<span class="productcarousel__fullscreen"></span>
<img src="https://cloudxyd.com/image/.../gtfrgrfrtg.jpg">
</div>
I tried this command without any success:
links = driver.find_elements_by_xpath("//*[contains(text(), 'url(//cloudxyd.com/image/')]")
After getting the xpath, you'll have to use get_attribute('src') on links to get the url
links = driver.find_elements_by_xpath("//*[contains(text(), 'url(//cloudxyd.com/image/')]")
url=links.get_attribute('src')
You may also use css selector.
link = driver.find_element_by_css_selector(".productcarousel__mainitem.slick-slide.slick-current.slick-active img").get_attribute("src")
use the below xpath :
//div[contains(#class, 'productcarousel__mainitem')]/img
and get the URL like this :
img_url = driver.find_element(By.XPATH, "//div[contains(#class, 'productcarousel__mainitem')]/img
").get_attribute('src')
print(img_url)
or if the above mentioned xpath highlights more than one entry in DOM, you could probably do something like this :
for img_url in driver.find_elements(By.XPATH, "//div[contains(#class, 'productcarousel__mainitem')]/img")
print(img_url.get_attribute('src'))

python selenium - Why cant I click this?

https://gyazo.com/9887635aa04d723758ba17b3413f72e1 - the html
the one named class="radio inline bundle-cta" is the one i want.
<label class="radio inline bundle-cta">
<input data-bind="checked: activeBundle" id="BundleType" name="BundleType" type="radio" value="Default">
Nej tak, jeg vælger en basisannonce uden tilvalg
<span class="uneditable-input price">Kr. <span data-price="0">0</span>,-</span>
<span class="bundle-cta-button">Vælg basisannonce</span>
</label>
error msg, when trying to click it:
Element is not clickable at point (604, 698). Other element would receive the click: <p style="line-height: 20px; margin: 0px 40px 0px 15px; padding: 0px;">...</p>
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64)
code I used to try to click it:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.class, "bundle-cta-button"))
# vælg gratis pakke
browser.find_element_by_class_name('bundle-cta-button').click()
sleep(0.75)
browser.find_element_by_xpath('//*[#id="content"]/div[2]/div/div/form/div/button').click()
Try to change spaces into dots, and find by CSS_selector like below:
browser.find_element_by_css_selector("radio.inline.bundle-cta").click()

Categories