I've managed to write a program that can navigate to the desired website, and click on the first textbox I want filled out. The problem i'm having is that the send_keys method i'm using is not filling out the desired textbox with 'Testing', and i'm receiving this error:
AttributeError: 'NoneType' object has no attribute 'find_elements_by_xpath'
Here is the code so far:
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions
driver = selenium.webdriver.Chrome(executable_path='path_to_chromedriver')
def get_url(url):
driver.get(url)
driver.maximize_window()
Wait(driver, 30).until(expected_conditions.presence_of_element_located
((By.ID, 'signup-button'))).click()
def fill_data():
sign_up = Wait(driver, 30).until(expected_conditions.presence_of_element_located
((By.XPATH,
'/html/body/onereg-app/div/onereg-form/div/div/form/section/section['
'1]/onereg-alias-check/ '
'fieldset/onereg-progress-meter/div[2]/div[2]/div/pos-input[1]'))).click()
sign_up.send_keys('Testing')
get_url('https://www.mail.com/')
# Find the signup element
fill_data()
find_elements_by_xpath` returns all matching elements, which is a list, so you need to loop through and get text attribute for each of the element
Also, in the fill data method you are trying to fill data for a signup form so you need to identify all elements on that form and process with the form your with your data.
Your xpath was incorrect while filling email Id, please update your xpath and try again
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait as Wait
# Open Chrome
driver = webdriver.Chrome(executable_path='path_to_chromedriver')
def get_url(url):
driver.get(url)
driver.maximize_window()
def fill_data():
Wait(driver, 30).until(EC.element_to_be_clickable
((By.ID, 'signup-button'))).click()
inputBox = Wait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "/html/body/onereg-app/div/onereg-form/div/div/form/section/section[1]/onereg-alias-check/fieldset/onereg-progress-meter/div[2]/div[2]/div/pos-input[1]/input")))
inputBox.send_keys('Testing')
get_url('https://www.mail.com/')
# Find the signup element
fill_data()
Related
I need to make a login script for the website https://cbdbene.com/
but when I try to send Keys to the email field, I get the error
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I have tried using,
login_email = browser.find_element_by_xpath("//input[#id='login_email']")
driver.execute_script("argument[0].setAttribute('value', 'abs#gmail.com');", login_email)
but that is also of no help,
Even clicking the element has no response,
login_email = browser.find_element_by_xpath("//input[#id='login_email']")
driver.execute_script("argument[0].click();", login_email)
I don't know how to fill this form. Can someone please explain me what am I doing wrong here ?
Try below solution :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 50)
driver.get("https://cbdbene.com/")
warning = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='modal-dismiss']//i//*[local-name()='svg']")))
driver.find_element_by_tag_name('body').send_keys("Keys.ESCAPE")
warning.click()
loginIcon = wait.until(EC.element_to_be_clickable((By.XPATH, "//li[3]//span[1]//div[1]//div[1]//*[local-name()='svg']")))
loginIcon.click()
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[4]//div[1]//div[1]//div[1]//div[1]//div[1]//div[1]//div[1]//span[1]//input[1]")))
inputBox.send_keys("Username")
Output:
If you use absolute xpath you will see 3 inputs elements are there with same property.
Induce WebDriverWait() and element_to_be_clickable() and use valid xpath.samething you have do with password.
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
driver=webdriver.Chrome()
driver.get("https://cbdbene.com/")
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-dismiss"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li.c-nav__list-item>span.c-nav__link"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"(//input[#id='login_email'])[last()]"))).click()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"(//input[#id='login_email'])[last()]"))).send_keys("user name")
Browser snapshot:
I am trying to scrape this website
https://script.google.com/a/macros/cprindia.org/s/AKfycbzgfCVNciFRpcpo8P7joP1wTeymj9haAQnNEkNJJ2fQ4FBXEco/exec
I am using selenium and python.I am not able to view entire page source,Basically i have to scrape the table inside it and click on next button,but the code of next and table not visible on page source.Here is my code
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get(link)
pass1 = browser.find_element_by_xpath("/html/body/div[2]/table[2]/tbody/tr[1]/td/div/div/div[2]/div[2]")
pass1.click()
time.sleep(30)
I am getting this error,NoSuchElementException.
There are two iframes present on the page, so you need to first switch on those iframe and then you need to click on the element.
And you can apply explicit wait on the element so that the script waits until the element is visible on the page.
You can do it like:
browser = webdriver.PhantomJS()
browser.get(link)
browser.switch_to.frame(driver.find_element_by_id('sandboxFrame'))
browser.switch_to.frame(driver.find_element_by_id('userHtmlFrame'))
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, "//div[contains(#class,'charts-custom-button-collapse-left')]//div"))).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
Here is my code so far, note that the web page loads up with a captcha. I countered this with adding a time.sleep so I can run tests. When I try to submit the form by submitting "Create new account", I get an error saying the element has no attribute for 'submit'. I tried finding the element using xpath, css_selectos, tags, class names, etc.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
elam = browser.find_element_by_css_selector("[value='Create new account']")
elam.Submit()
If you are trying to click on Create new account button after filling information then please find below xpath to click on it
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()
Another solution with action class
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
submit = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//input[#class='button radius charcheck-submit']")))
actionChains.move_to_element(submit).click().perform()
Working code
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
# browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()
I have a page which loads dynamic content with ajax and then redirects after a certain amount of time (not fixed). How can I force Selenium Webdriver to wait for the page to redirect then go to a different link immediately after?
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome();
driver.get("http://www.website.com/wait.php")
You can create a custom Expected Condition to wait for the URL to change:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(driver, 10)
wait.until(lambda driver: driver.current_url != "http://www.website.com/wait.php")
The Expected Condition is basically a callable - you can wrap it into a class overwriting the __call__() magic method as the built-in conditions are implemented.
There are several ExpectedConditions that can be used along with ExplicitWait to handle page redirection:
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
Wait for new page title with title_is(title_name):
wait.until(EC.title_is('New page Title'))
Wait for current URL to be changed to any other URL with url_changes(exact_url):
wait.until(EC.url_changes('https://current_page.com'))
Wait for navigating to exact URL with url_to_be(exact_url):
wait.until(EC.url_to_be('https://new_page.com'))
Also
url_contains(partial_url) could be used to wait for URL that contains specified substring or url_matches(pattern) - to wait for URL matched by passed regex pattern or title_contains(substring) - to wait for new title that contains specified substring
It's a good practice to "wait" for unique (for new page) element to appear.
You may use module expected_conditions.
For example, you could wait for user logo on signing in:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get('http://yourapp.url')
timeout = 5
try:
logo_present = EC.presence_of_element_located((By.ID, 'logo_id'))
WebDriverWait(driver, timeout).until(logo_present)
except TimeoutException:
print "Timed out waiting for page to load"
I am trying to post something on http://indianrailforums.in using selenium script. I am able to login and reach this page: http://indiarailinfo.com/blog using the selenium script, but after I click post button I am not able to send text in the text area.
This is my code:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://indiarailinfo.com")
element = driver.find_element_by_name("e")
element.send_keys("yagyank#gmail.com")
element = driver.find_element_by_name("p")
element.send_keys("suvrit")
element.submit()
driver.get("http://indiarailinfo.com/blog")
element = driver.find_element_by_link_text('Post')
element.click()
element = driver.find_element_by_xpath("/html/body/div[1]/div[5]/table/tbody/tr/td[1]/div[1]/div[1]/div/div/form/textarea")
#element.sendKeys(Keys.HOME + "abc");
#element = driver.find_element_by_name("TrainBlog")
element.send_keys("suvrit")
#driver.quit()
EDIT: Issue solved by using submit button and using driver.implicitly_wait(10) just before calling xpath
I was able to post with PhantomJS driver. It should work with Firefox either.
My code is:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.PhantomJS()
driver.get("http://indiarailinfo.com")
element = driver.find_element_by_name("e")
element.send_keys("yagyank#gmail.com")
element = driver.find_element_by_name("p")
element.send_keys("suvrit")
element.submit()
driver.get("http://indiarailinfo.com/blog")
element = driver.find_element_by_link_text('Post')
element.click()
element = driver.find_element_by_xpath("/html/body/div[1]/div[5]/table/tbody/tr/td[1]/div[1]/div[1]/div/div/form/textarea")
print element.tag_name
#element.sendKeys(Keys.HOME + "abc");
#element = driver.find_element_by_name("TrainBlog")
element.send_keys("chuh-pook")
element.submit()
#driver.quit()
The problem is that you have not submitted the form. Either call submit():
element = driver.find_element_by_xpath("/html/body/div[1]/div[5]/table/tbody/tr/td[1]/div[1]/div[1]/div/div/form/textarea")
element.send_keys("suvrit")
element.submit() # it would find the parent form and submit it
Or, click the post button:
post = driver.find_element_by_css_selector('input.postbtn')
post.click()