I'm trying to click on a box that has the following HTML code:
<li>
<input id="mce-group[166]-166-0" type="checkbox" value="1" name="group[166][1] >
<label for="mce-group[166]-166-0">I agree</label>
</li>
I've tried it all: id, name, xpath, text,...
Running something like this:
select_box = driver.find_element_by_xpath('//*[#id="mce-group[166]-166-0"]')
select_box.click()
I get this error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="mce-group[166]-166-0" name="group[166][1]" type="checkbox"> could not be scrolled into view
To click on the <input> element associated with the text I agree you need 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, "label[for=\"mce-group[166]-166-0\"]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for=\"mce-group[166]-166-0\"]"))).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
Related
Error Msg:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}
I have tried with tagname as button too.
The HTML for this:
<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
autocomplete="off" method="post" target="_top" novalidate="novalidate">
<p class="username">To reset your password, enter your Salesforce username.</p>
<input type="hidden" name="locale" value="in" />
<div class="verifyform">
<label for="un" class="label">Username</label>
<input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
name="un" value="" autofocus />
<input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
<input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16" value="Cancel" >
</div>
<input type="hidden" name="lqs" value="locale=in" />
<div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
<a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
<p class="small">Video: Need Help Logging In?</p>
</form>
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(service=Service("C:\\Users\\Dell\\Desktop\\Selenium\\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()
The element Cancel doesn't have a innerText rather the value attribute is set as Cancel
<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">
Solution
To click element instead of presence_of_element_located() you need 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[value='Cancel']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='Cancel']"))).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
Have you tried the following code:
driver.find_element(By.XPATH, '//*[#id="forgotPassForm"]/div[1]/input[3]').click()
To get the basic XPath you can inspect element by right clicking on the web page and selecting 'Elements'. You can then right click the button and then 'Copy > Copy XPath'. This will allow you to get the very basics working before then refactoring and using a more robust piece of xcode.
See screenshot:
The element you are trying to access is input element NOT button element.
Use following xpath to identify the element.
driver.find_element(By.XPATH, "//input[#name='cancel' and #value='Cancel']").click()
To handle dynamic element use WebDriverWait() and wait for element to be clickable.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='cancel' and #value='Cancel']"))).click()
You need to import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
I am trying to click on the "Training material statistics" by using the following code in Python, but it didn't work:
WebDriverWait(driver,20)\
.until(EC.element_to_be_clickable((By.XPATH,'//*[#id="report-navigation"]/div[2]')))\
.click()
HTML:
<div id="report-navigation">
<div class="report-nav-btn active" onclick="Report.changeGrid(this, 'report-users-grid')">
User statistics
<div class="report-nav-arrow active"></div>
</div>
<div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-objects-grid')">
Training material statistics
<div class="report-nav-arrow"></div>
</div>
<div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-deliverables-grid')">
Learner assignments
<div class="report-nav-arrow"></div>
</div>
</div>
HTML Snapshot:
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 Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#report-navigation div[onclick*='report-objects-grid']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='report-navigation']//div[contains(#onclick, 'report-objects-grid')]"))).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
trying to build a bot to my own services, but while i'm trying to run its crashs
login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "//div[#class='loginRegisterButtons sb-login-register-buttons clearfix ember-view']//a[#class='btn signin-btn sbLoginBtn buttons1571052392296']")))
login_button.click()
the html
<div class="loginRegisterButtons sb-login-register-buttons clearfix ember-view">
<div>
<a class="btn signin-btn sbLoginBtn buttons1571052392296">
<span class="btf-text">ENTRAR</span>
</a>
</div>
</div>
The desired element is a Ember.js enabled 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, "a.btn.signin-btn.sbLoginBtn > span.btf-text"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='btf-text' and text()='ENTRAR']"))).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
This is the HTML code in question:
<textarea rows="1" cols="1" name="text" class=""></textarea>
This is my code:
msgElem = driver.find_element_by_css_selector("textarea[name='text']")
driver.execute_script("arguments[0].click();", msgElem)
driver.execute_script("arguments[0].value = 'Whats up mate, how you doin';", msgElem)
msgElem.submit()
The code executes and nothing happens. I assume it selects the textarea but doesn't type nothing into it? or nothing happens at all. It also finds the element so I assume I don't need to wait for the textarea to be visible.
When I don't use js and just do
msgElem = driver.find_element_by_css_selector("textarea[name='text']")
msgElem.send_keys('Whats up mate, how you doin')
It gives me ElementNotInteractableException.
This error message...
ElementNotInteractableException
...implies that the WebDriver instance was unable to interact with the desired element as the Element was Not Interactable.
Analysis
The Locator Strategy which you have used actually identifies two elements within the HTML DOM and the parent element of the first matching element contains the attribute style="display:none" as follows:
<form action="#" class="usertext cloneable warn-on-unload" onsubmit="return post_form(this, 'comment')" style="display:none" id="form-dyo">
<input type="hidden" name="thing_id" value="">
<div class="usertext-edit md-container" style="">
<div class="md">
<textarea rows="1" cols="1" name="text" class=""></textarea>
</div>
Hence you see ElementNotInteractableException.
Solution
To send a character sequesce to the desired element you need 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.title + div textarea[name='text']"))).send_keys("Sowik")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='message']//following::div[1]//textarea[#name='text']"))).send_keys("Sowik")
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
Trying to build out a webscraper but for the life of me I can't seem to get this link to click. I always get an Unable to Locate Element error no matter what I try. Here's the HTML:
<td colspan="2" width="100" height="100">
<a href="place.php?whichplace=desertbeach">
<img src="https://s3.amazonaws.com/images.kingdomofloathing.com/otherimages/main/map7beach.gif" alt="Desert Beach" title="Desert Beach" width="100" height="100" border="0">
</a>
</td>
I'm trying to click the link associated with the a href link. Tried find element by css, xpath and more but none can seem to find it. Any help?
To click on a link induce WebDriverWait and element_to_be_clickable() and following CSS selector.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="place.php?whichplace=desertbeach"]'))).click()
OR XPATH
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[#href="place.php?whichplace=desertbeach"]'))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
UPDATE
The table is present inside and iframe you need to switch it first in order to click on the element.Induce WebDriverWait() and frame_to_be_available_and_switch_to_it() and use the css selector.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, 'frame[src="main.php"]')))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="place.php?whichplace=desertbeach"]'))).click()