How to use Python Selenium - python

I have a piece of HTML Code, which offers you to put your email in an input field after hitting a button (after clicking the button the textfield appears):
<div class="column-left w-col w-col-5">
<label class="radio-button-field-2 left w-radio">
<input type="radio" id="paypal" name="payout" value="paypal" data-name="payout" required="" class="w-form-formradioinput radio-button w-radio-input">
<span for="paypal" class="radio-button-label-2 w-form-label">PayPal-Konto</span>
</label>
</div>
<div class="block-payout-paypal" style="">
<input type="email" class="text-field-2 w-input" maxlength="256" name="paypal-email-2" data-name="Paypal Email 2" placeholder="max.moritz#beispiel.com" id="paypal-email" required="">
</div>
My Goal now is to write a script which allows me to click on the button and then file out the input field. The website is: https://gruenkraft-zufriedenheit.de/ And the button I'm looking for is called: PayPal-Konto

When (PayPal-Konto) button is clicked, you would see a input, you can locate that with the help of below code.
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://gruenkraft-zufriedenheit.de/")
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='PayPal-Konto']"))).click()#ActionChains(driver).move_to_element(wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='PayPal-Konto']")))).click().perform()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[name='paypal-email-2']"))).send_keys('some-email#gmail.com')
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

This worked for me:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://gruenkraft-zufriedenheit.de/ ')
element = driver.find_element_by_tag_name('button_tag_name_youre_looking_for')
element.click()
from then I use beautifulsoup to go on with what I needed to do
Hope this helps : )

You can try with below code too.
driver.find_element_by_xpath("//span[text()='PayPal-Konto']").click()
driver.find_element_by_id("paypal-email").send_keys("text")
Updated as per comments.

Related

How to use derive an X-Path from a specific HTML attribute to use with Selenium

Background:
I have the following HTML code that I am trying to get a XPATH to use with Selenium
<div class="btn-group">
<a type="button" class="btn btn-primary btn-user" onclick="AiD('182030801')" href="/download.pdf?id=182030801&h=917901e6659ad5eb53970aecf687b53e&u=cache&ext=pdf" target="_blank" style="border-top-left-radius: 3px;border-bottom-left-radius: 3px;">
<i class="fas fa-cloud-download-alt" aria-hidden="true" style="margin-right: 9px;margin-left: 2px;font-size: 25px;vertical-align: middle;color: #119802;"></i>Download ( PDF )
</a>
[...]
</div>
Code:
What have I tried to do with Python's Selenium is the following, however I cannot quiet get it to work without Python throwing an error:
browser.find_element(By.XPATH, "//div[#class='btn-group']/a").click()
browser.find_element(By.XPATH, "//div[#class='btn tn-primary btn-user']").click()
Error:
When I run the above snippet of code, the following error is produced and script crashes thereafter:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn tn-primary btn-user']"}
Problem:
The error states "no such element", however the element clearly exists "class="btn btn-primary btn-user""
Question:
How can I use Seleniums XPATH to "see" class="btn btn-primary btn-user" and click it to download a PDF?
Link:
PDFDrive
USe Xpath //div/a[#class='btn btn-primary btn-user']
Make sure the intended element is not under an iFrame. If it is then first you need to switch into that iFrame and then have to perform the action
Make sure you are using proper synchrnization and your element is ready. Introduce explicit wait as below:
driver.get('https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='btn-group']/a"))).click()
time.sleep(10) // it doesn't recommanded to put hardcoded wait but for debugging purpose you can check
Import below packages for this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
You are using "div" tag but I can see respective class = btn btn-primary btn-user is inside "a" tag
try below code :
browser.find_element(By.XPATH, "//a[#class='btn tn-primary btn-user']").click()
Or you can go with another locator as well.
CSS : a.btn.btn-primarybtn-user
So you need to wait for the element to be clickable. Triggered the download.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = Options()
driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
url = 'https://www.pdfdrive.com/querying-xml-xquery-xpath-and-sqlxml-in-context-d38665640.html'
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.btn.btn-primary.btn-user'))).click()

How can make (style =“display: none;”) element visible in python selenium

<div class="clearfix entry_but_bar_out clearfix" style="display: none;">
<div class="entry_but_bar_in clearfix">‹ yanıtlaeditsilmesajgammazla#208281235<i class="ssicon-facebook"></i>
<i class="ssicon-twitter"></i>
</div><!-- entry_but_bar_in clearfix -->
</div>
i'm trying to make elements visible and click on it . But it doesn't work ,
clicking = browser.find_element_by_xpath('//*[#id="middle-block"]/ol/li/div/div[3]/div[2]/span')
browser.execute_script("removeAttribute('display:none');",clicking)
clicking.click()
Please try the below code and let me know if it works for you.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
# Put your URL
driver.get('Your URL')
time.sleep(5)
Target_Div = driver.find_element_by_xpath('//div[contains(#class,"clearfix entry_but_bar_out clearfix")]')
driver.execute_script("arguments[0].setAttribute('style','display:\"true\"')", Target_Div)
Use the above code and make correction in your code.

How to get html element inside button?

<button type="button" name="abc" id="abc" class="bp" onmouseover="this.className = 'bp bph'" onmouseout="this.className = 'bp'" onclick="oCV_NS_.promptAction('finish')" style="font-family:"Arial";font-size:9pt">
<span tabindex="0">GENERATE REPORT</span>
</button>
I want to click this button and tried few codes but nothing worked
tried:
driver.find_element_by_id("abc").click();
driver.find_element(By.ID, "abc")
element_by_name also tried
Please try below code and also check if button is not part of iframe:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
button= wait.until(EC.visibility_of_all_elements_located((By.ID, "abc")))
button.click()
Try use with the below xpath:
driver.find_element_by_xpath("//button[#id='abc']/span").click()

How can I send keys to a hidden password box

I am trying to login to a site www.bet365.com
I am able to send the login information for the username, however the password box is split into both a visible and hidden element for security.
How am I able to send the password to the box in order to automate login?
I have tried accessing the visible element however when passed not all characters of the password are received.
When attempting to pass to the hidden element no password is visibly sent.
I have also tried clicking the visible element before sending but this also did not work.
wait=WebDriverWait(driver,3)
userele=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_UserNameWrapper input.hm-Login_InputField[type="text"]')))
userele.send_keys('xyz#gmail.com')
passwdele=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper input.hm-Login_InputField.Hidden[type="password"]')))
passwdele.send_keys('xxxxxxxxxxx')
btnelement=wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper button.hm-Login_LoginBtn')))
btnelement.click()
Here is the main code for the elements on the bet365 page
<div class="hm-HeaderModule_UserAdmin ">
<div class="hm-Login ">
<div class="hm-Login_UserNameWrapper ">
<input type="text" class="hm-Login_InputField ">
<div class="hm-Login_InputText ">Join Now</div></div>
<div class="hm-Login_PasswordWrapper ">
<input type="text" class="hm-Login_InputField ">
<input type="password" class="hm-Login_InputField Hidden ">
<button tabindex="0" class="hm-Login_LoginBtn ">GO</button>
<div class="hm-Login_InputText ">Lost Login?</div></div></div></div>
Only the username passing currently works.
Well, you can send the password in visible input like this :
Code :
driver = webdriver.Chrome(executable_path = r'chromedriverPath')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.bet365.com/#/HO/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#title='New Customer Bonus']"))).click()
userele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.hm-Login_UserNameWrapper input.hm-Login_InputField[type='text']")))
userele.send_keys('xyz#gmail.com')
passwdele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.hm-Login_PasswordWrapper input.hm-Login_InputField[type='text']")))
passwdele.send_keys('xxxxxxxxxxx')
btnelement = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.hm-Login_PasswordWrapper button.hm-Login_LoginBtn')))
btnelement.click()
imports would be :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Let me know, if you are facing any problem.
You need to avoid invoking send_keys() to a hidden password box in all possible ways. However inducing WebDriverWait for the first element on the page you want to interact would be enough and you don't need to invoke WebDriverWait multiple times when interacting with <input> elements. You can try either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.hm-Login_UserNameWrapper>input.hm-Login_InputField[type='text']"))).send_keys('xyz#gmail.com')
driver.find_element_by_css_selector("div.hm-Login_PasswordWrapper>input.hm-Login_InputField:not(.Hidden)").send_keys("hello")
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='hm-Login_UserNameWrapper ']/input[#class='hm-Login_InputField ' and #type='text']"))).send_keys('xyz#gmail.com')
driver.find_element_by_xpath("//div[#class='hm-Login_PasswordWrapper ']/input[contains(#class, 'hm-Login_InputField') and not(#class='Hidden')]").send_keys("Jp1875")
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 in Python: "NoSuchElementException: Message: no such element: Unable to locate element"

I tried typing 'abc' in the first block of id and 'cdef' in the second block of password.
However, the error code at the bottom comes up.
from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')
I added an implicit wait to prevent the code from executing before the page fully loads.
driver.implicitly_wait(30)
Code for adding username and password is as below:
driver.find_element_by_name('id').send_keys('abc')
driver.find_element_by_name('pw').send_keys('cdef')
But I am getting the below error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}
The 'No Such Element' exception usually comes when web driver can't see the element you are trying to perform an action on.
Reasons can be:
your ID or Name or XPath or CssSelector can be wrong.
Your element might be inside an an iframe so that web driver can't see or detect it. Switch to an iframe through Selenium and python
Your element is taking time to appear on the UI, so you can use an explicit wait to solve this. See 5. Waits
The username and password fields are within an frame, so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
from selenium import webdriver
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(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe')
driver.get("http://sugang.korea.ac.kr")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc')
driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")
Browser Snapshot:
Add explicitly wait
from selenium.webdriver.support import expected_conditions as EC
userNameElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "id"))
userNameElement.send_keys('abc')
pwdElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "pwd"))
pwdElement.send_keys('cdef')
Here, I am expecting that your locators are correct.
It is in a frame which you need to switch to first. Also, use ids where possible as they are faster.
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
url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()
The site you are trying to access does not have an element with a tag name id. Examine the site carefully.
<input name="id">
If the input has an id value, try this;
driver.find_element_by_id("id")
Example Use:
HTML:
<div class="form-group">
<input class="form-control" name="username">
</div>
<div class="form-group">
<input class="form-control" name="password" type="password">
</div>
<button id="btn-login" type="submit">Enter</button>
Python:
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("your_username")
password.send_keys("your_password")
driver.find_element_by_id("btn-login").click()

Categories