I made script that automatically picks a product from kith.com and go to the checkout:
element3 = driver.find_element_by_xpath('//*[#id="CheckoutData_BillingFirstName"]')
element3.send_keys("My First Name")
Kiths website:
<div class="col-sm-8 col-xs-12 fval">
<input class="form-control input-validation-error" data-val="true" data-val-countryaddressvalidation="" data-val-countryaddressvalidation-countryaddressvalidationpropname="" data-val-required="Billing First Name is required" data-val-unsupportedcharacters="Please use English characters only" data-val-unsupportedcharacters-unsupportedcharacterspattern="^[A-Za-z0-9,""'`\s#&%$#\*\(\)\[\]._\-\s\\/]*$" id="CheckoutData_BillingFirstName" maxlength="40" name="CheckoutData.BillingFirstName" placeholder="First Name" type="text" value=""><span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
</div>
How can I locate the input form and send keys to it?
I am getting the same error every time :
Unable to locate element: {"method":"xpath","selector":"//*[#id="CheckoutData_BillingFirstName"]"}
Your target element inside a <frame>:
<iframe src="https://fs.global-e.com/Checkout/v2/f77781eb-a7c0-43e2-822a-3ca96e8658f0?gaSesID=361925132.674171348.583&gaMerchantClientInfo=undefined#undefined&chkcuid=3ef950c0-4c7d-4cfe-bab5-3a1ed7035318&isNECAllowed=true&vph=631&ift=87" class="Intrnl_CO_Container" id="Intrnl_CO_Container" name="Intrnl_CO_Container" allowtransparency="true" width="100%" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" height="1000px" style="height: 2196px;"></iframe>
You need to switch it first:
#after clicked checkout button
time.sleep(10)
driver.switch_to.frame(driver.find_element_by_id("Intrnl_CO_Container"))
time.sleep(10)
element3 = driver.find_element_by_xpath('//*[#id="CheckoutData_BillingFirstName"]')
element3.send_keys("My First Name")
But there is a better way to wait in selenium, for detail you can read the #pcalkins suggestion.
After clicked checkout button, you can add following code:
#after clicked checkout button
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'Intrnl_CO_Container')))
element3 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="CheckoutData_BillingFirstName"]'))).click()
element3.send_keys("My First Name")
Please import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="modal-content">
<div class="modal-header">
<h5 id="add-title" class="modal-title">Add a text</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<section id="add-popup-card-body">
<form id="add-form"><div class="sm-form modal-body">
<h6 id="add-popup-card-body-subtitle" class="card-subtitle mb-2 text-muted">Please enter text</h6>
<input type="text" id="add-input-form" class="sm-form modal-body form-control validate" pattern="\S+" style="text-transform:uppercase" maxlength="32" placeholder="tag" onkeyup="this.value = this.value.toUpperCase();" required="">
<div><small>*Spaces are not allowed</small></div>
</div>`enter code here`
<div class="modal-footer justify-content-center">
<input type="submit" class="btn btn-primary">
</div></form>
</section>
</div>
</body>
</html>
All that I need is to find a way to make Selenium test close the modal. I have tried these so far and none worked:
self.driver.findElement(By.className("close")).click()
self.driver.findElement(By.xpath("//button[#class = 'close']")).click()
self.driver.find_element(By.CSS_SELECTOR, 'button[class="close"]').click()
There are basically 4 ways to click in Selenium.
I will use this xpath
//button[#class='close' and #data-dismiss='modal' and text()='×']
Code trial 1:
time.sleep(5)
driver.find_element_by_xpath("//button[#class='close' and #data-dismiss='modal' and text()='×']").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='close' and #data-dismiss='modal' and text()='×']"))).click()
Code trial 3:
time.sleep(5)
button = driver.find_element_by_xpath("//button[#class='close' and #data-dismiss='modal' and text()='×']")
driver.execute_script("arguments[0].click();", button)
Code trial 4:
time.sleep(5)
button = driver.find_element_by_xpath("//button[#class='close' and #data-dismiss='modal' and text()='×']")
ActionChains(driver).move_to_element(button).click().perform()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
To click on the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "button.close[data-dismiss='modal']").click()
Using xpath:
driver.find_element(By.XPATH, "//button[#class='close' and #data-dismiss='modal'][text()='×']").click()
Ideally, as the desired element is a Modal Dialog Box to click on the clickable 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, "button.close[data-dismiss='modal']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='close' and #data-dismiss='modal'][text()='×']"))).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
Thanks for the help. I have tried all the above solution but none worked. I was getting similar errors. After some reading I called an id that was above the "modal-content" and added 3 div . By using this code :
self.driver.find_element_by_xpath("//*[#id='add-tag popup']/div/div/div/button").click()
I am making a program that will automatically post image on LinkedIn.
It is only able to click on the 'Photo', after that it does't click on anything.
I found that when I try to select the 'select images to share' button, it does't work.
So, I manually clicked on 'select images to share' button and in the second line of the HTML code, this popped from nowhere :
data-artdeco-is-focused="true"
And, when I try to copy it by clicking on HTML code on the browser, it hides the code.
This is the HTML code: (<data-artdeco-is-focused="true"> appeared in the end of second line of the HTML code below)
<div class="image-sharing-detour-container">
<input id="image-sharing-detour-container__file-input" class="image-sharing-detour-container__media-button visually-hidden" name="file" multiple="" filecountlimit="9" accept="image/gif,image/jpeg,image/jpg,image/png" type="file">
<div class="image-sharing-detour-container__upload-media-button">
<label for="image-sharing-detour-container__file-input" class="artdeco-button artdeco-button--tertiary">
Select images to share
</label>
</div>
<!----> <div class="share-box-footer ">
<div class="share-box-footer__main-actions">
<button id="ember421" class="artdeco-button artdeco-button--2 artdeco-button--secondary ember-view" type="button"><!---->
<span class="artdeco-button__text">
Cancel
</span></button>
<button disabled="" id="ember422" class="ml2 artdeco-button artdeco-button--2 artdeco-button--primary artdeco-button--disabled ember-view" type="button"><!---->
<span class="artdeco-button__text">
Done
</span></button>
</div>
</div>
</div>
This is my python code so far;
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep as slp
#open browser & visit linkedin
driver = webdriver.Chrome("/Users/....../chromedriver")
driver.get('https://www.linkedin.com/')
#enter user/pass & click submit
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_key']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_password']")))
username.clear()
username.send_keys("user#gmail.com")
password.clear()
password.send_keys("mypasswordhere")
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
#go to company page
checkpoint = driver.get('https://www.linkedin.com/company/1234567/admin/')
#click on photo and add an image
addphoto = driver.execute_script("arguments[0].click();", driver.find_element_by_xpath("//button[#aria-label='Add a photo']"))
(Edit: this is the //button[#aria-label='Add a photo'] in th HTML code)
Cannot click button id="close in modal window.
Trying all xpaths like:
//button[#data-dismiss='modal']
//button[#id='close'], //button[#type='button']
//button[contains(.,'Закрыть')]
//button[contains(#data-dismiss,'modal')]
//button[contains(#id,'close')]
Also trying to combine xpaths, but still not working
Code:
<div id="idCardGroupChangeStatusResult" class="modal fade in" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
<div class="modal-dialog st-modal-dialog" style="width: 600px; padding-top: 250px;">
<div class="modal-content">
<div class="modal-header st-pad-normal">
<div class="modal-body">
<div class="modal-footer">
<button id="close" class="btn btn-default btn-sm" type="button" data-dismiss="modal"> Закрыть </button>
</div>
</div>
</div>
</div>
Css not working to
Any ideas?
As the element with text as Закрыть is within a Modal Dialog Box so to locate the desired 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, "button.btn.btn-default.btn-sm#close[data-dismiss='modal']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-default btn-sm' and #id='close'][#data-dismiss='modal']"))).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
If element_to_be_clickable not works, try code below to check if there's more than one close buttons on the page. You can use code below to filter by visible or visible and latest one and click on it.
close_buttons = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#idCardGroupChangeStatusResult #close")))
# check how many buttons in on the HTML, you can try "visibility_of_all_elements_located"
print(len(close_buttons))
visible_buttons = [close_button for close_button in close_buttons if close_button.is_displayed()]
visible_buttons_len = len(visible_buttons)
print(visible_buttons_len)
visible_buttons[visible_buttons_len - 1].click()
Does the message disappears?
Update, message window disappear:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
actions = ActionChains(driver)
#...
status_message = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "#idCardGroupChangeStatusResult")))
actions.move_to_element(status_message).perform()
# here you can get text from message window, check/assert ..
status_message.find_element_by_css_selector("#close").click()
I have one button, and I want to click on this button , I do
login_form = driver.find_element_by_xpath("/html/body/div/h1/div[1]").click();
my code :
driver = webdriver.Firefox()
driver.get('http://www.textdet.com/')
e = driver.find_element_by_id("imagefile")
e.send_keys("/home/brm17/Desktop/ProjetFinDetude/image.png")
login_form = driver.find_element_by_xpath("/html/body/div/h1/div[1]").click();
But I get:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: /html/body/div/h1/div[1]
how to click on button Download bounding boxes on python by selenium , the html
<h1 style="position: relative">Scene Text Detection Demos
<div aria-label="..." role="group" class="btn-group" style="position: absolute; bottom: 10px; right: 0px;">
<!--<button id="toggle_text_propoals" type="button" class="btn btn-success btn-sm">Show text proposals</button>-->
Download bounding boxes
</div>
</h1>
The button is in <a> tag, not <div> tag. You also have only one <div> so div[1] is invalid. You can search by the text
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//a[contains(., "Download bounding boxes")]')))
button.click();
Or by class since you have only one button so class btn should be unique.
driver.find_element_by_class_name('btn').click(); # or 'btn-success' or 'btn-sm'
By the way, click() doesn't return anything, you can't assign it to login_form.
The button is under an <a> tag so we write code for Xpath:
driver.find_element_by_xpath("//a[. = 'Download bounding boxes']").click()
so it finds the text is "Download bounding boxes" on the website and clicks on it.
In the code above // a is written because the button was inside the <a> tag we can write //span,//div as well if the code was under a span or divtag.
I have the following Selenium Test for Python/Django application:
class EmailRecordsTest(StaticLiveServerTestCase):
def test_can_store_email_and_retrieve_it_later(self):
self.browser.get(self.live_server_url)
emailbox = self.browser.find_element_by_xpath("//form[#class='pma-subscribe-form']/input[1]")
self.assertEqual(emailbox.get_attribute("placeholder"), 'Enter your Email')
print("tested until here")
print("The placeholder: ", emailbox.get_attribute("placeholder"))
print(emailbox)
emailbox.send_keys('vio#mesmerizing.com')
First occurance of emailbox is clearly identified as seen from the print runs and assert Equal for placeholder. The last instance of emailbox.send_keys throws following error:
selenium.common.exceptions.ElementNotVisibleException: Message:
Element is not currently visible and so may not be interacted with
Cannot find why the same element become Not Visible when using with send_keys.
The Html code being tested is as below:
<!-- Start footer -->
<footer id="pma-footer">
<!-- start footer top -->
<div class="pma-footer-top">
<div class="container">
<div class="pma-footer-top-area">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3">
<div class="pma-footer-widget">
<h4>News letter</h4>
<p>Get latest update, news & offers</p>
<form class="pma-subscribe-form">
<input id="subscribe-email" type="email" placeholder="Enter your Email">
<button class="btn btn-danger btn-md" type="submit">Subscribe!</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- end footer top -->
Kindly help.
Actually find_element returns element which would be present on the DOM no matter it's visible or not and you can get attribute of this element as well but send_keys does an action on element and selenium does action only visible element, So you need to be sure before doing action on element that it's visible using WebDriverWait as below :-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
emailbox = wait.until(EC.visibility_of_element_located((By.ID, "subscribe-email")))
#do your all stuff before send keys
# now use send_keys
emailbox.send_keys('vio#mesmerizing.com')
Edited :- If you are still unable to interact with element try using execute_script() to set value as below :-
emailbox = wait.until(EC.presence_of_element_located((By.ID, "subscribe-email")))
#do your all stuff before send keys
# now use execute_script
driver.execute_script("arguments[0].value = 'vio#mesmerizing.com'", emailbox)
Another option which worked in this case is that you scroll to the specific element (which was at the bottom of the page)and then use send_keys it works.
emailbox = self.browser.find_element_by_xpath("//form[#class='mu-subscribe-form']/input[1]")
self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
emailbox.send_keys('vio#mesmerizing.com')