Selenium using python - python

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()

Related

Button click using selenium webdriver youtbe example

When I run the code, it opens the browser, but I want to click the reject all button when the cookie banner comes from YouTube. I tried using class and a link text. It did not work.
I will appreciate any help.
from selenium import webdriver
from selenium.webdriver.common.by import By
url = 'https://www.youtube.com/'
driver = webdriver.Chrome(executable_path="C:\\Users\\donner\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get(url)
driver.implicitly_wait(100)
continue_link = driver.find_element(By.LINK_TEXT, "Reject all")
continue_link.click()
driver.implicitly_wait(100)
content = driver.find_element(By.CLASS_NAME, '.style-scope.ytd-button-renderer.style-primary size-default')
content.click()
Try the below xpath with the given code
//*[normalize-space()='Reject all']
OR
//*[contains(text(),'Reject all')]
The Code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))
element.click();
The above soultion work if there is no iframe
In case there is iframe, than you need to follow below
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe")))
driver.find_element(By.XPATH, "yourXpath").click()

Selenium: Cannot fill the input field with Send Keys and Executing Script

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:

Python Selenium not able to find element for Create New Account button on website

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()

Filling out text box after navigating to it using Selenium?

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()

python selenium click on button xpath error

I am trying to scrape an airbnb listing. I cant figure out a way to get the full list of amenities other than clicking on "more". I am using selenium to simulate the click, but it does nt seem to work.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
url = 'https://www.airbnb.com/rooms/4660676'
driver = webdriver.Firefox()
driver.get(url)
elem = driver.find_element_by_xpath('//a[#class="expandable-trigger-more"]')
actions.click(elem).perform()
The XPath itself is correct, but you have not defined the actions:
from selenium.webdriver.common.action_chains import ActionChains
elem = driver.find_element_by_xpath('//a[#class="expandable-trigger-more"]')
actions = ActionChains(driver)
actions.click(elem).perform()
Note that you can simply use the WebElement's click() method instead:
elem = driver.find_element_by_xpath('//a[#class="expandable-trigger-more"]')
elem.click()
Works for me.
If you are getting NoSuchElementException error, you might need to wait for the link to be clickable via WebDriverWait and element_to_be_clickable.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[#class="expandable-trigger-more"]'))
)
element.click()
A very simple way of achieving this is given below
driver.find_element_by_xpath('//a[#class="expandable-trigger-more"]').click()
It works for me hope will work for you as well.

Categories