How can I click this button using selenium? - python

<button name="main" type="submit" class="mainfont">
التسجيل</button>
XPath:
/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table[1]/tbody/tr/td[18]/font/button
Full XPath:
/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table[1]/tbody/tr/td[18]/font/button
How can I click this button in selenium? I have tried different methods but none worked, some notes:
There are different submit buttons called "main" with mainfont class
I tried using the XPath but the function also requires me to use the text inside the button, which is in arabic characters that split into two lines in python text editor, which ofcourse does not work as it needs to be in 1 line, but if I erase the space and make it into one code line, it won't work (probably because it is written in two lines in the HTML code?)
Edit: added code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
#
path = "XXXX"
driver = webdriver.Chrome(path)
#
driver.get("XXXX")
driver.maximize_window()
#
login1 = driver.find_element_by_name("liun")
login1.send_keys("XXXX")
time.sleep(2)
login1.send_keys(Keys.RETURN)
#
try:
element1 = WebDriverWait(driver, 100).until(
EC.presence_of_element_located((By.title_is, "XXXX"))
)
finally:
login2 = driver.find_element_by_name("gsw")
login2.send_keys("XXXX")
login2.send_keys(Keys.RETURN)
pass
# THE CODE BELOW DOES NOT WORK
html = '''
<button name="main" type="submit" class="mainfont">\nالتسجيل</button>
'''
driver.get("data:text/html;charset=utf-8," + html)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='\nالتسجيل']"))).click()

I made the HTML that you've provided and could create a xpath with text and clicked on it using Explicit waits :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
html = '''
<button name="main" type="submit" class="mainfont">التسجيل</button>
'''
driver.get("data:text/html;charset=utf-8," + html)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='التسجيل']"))).click()
print('Done successfully clicking')
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

How to press button?

I'm pretty new with the selenium module in python and I'm not able to press a button.
Here the HTML-Code:
<!-- more tags -->
<a href="#">
<img src="flag-en.png" title="English" alt="English"> English
</a>
<!-- more tags -->
Here's a minimal example:
import selenium.webdriver
import selenium.webdriver.chrome
import selenium.webdriver.chrome.options
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def get_driver():
chrome_options = selenium.webdriver.chrome.options.Options()
return selenium.webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver = get_driver()
driver.get(url)
# you can comment this out, if the first language which is selected isn't english
# otherwise this time is used to manually change the language to a non-english
# language to test, if it really selects the correct button
time.sleep(2)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='btn-group lang-sel']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#title='English']"))).click()
what am I doing wrong?
Context
I want to automatically select the language of the website.
It seems to be your locator strategy is correct. You can shorten your xpath expression as follows:
pos= WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//ul[#class="dropdown-menu"]/li[1]/a))).click()
There are may be spaces before of after the text inside the web element, so you can try this:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#title='English']"))).click()
Or
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(.,'English')]"))).click()

How to select prompts on a dropdown menu using Selenium and Python

My goal is to search for an item on the microcenter store. However, I need to select a store to choose in a dropdown menu so that I can get the proper search results. I cannot figure out how to do this. For some reason driver.find_elements_by_class_name does not work to open up the dropdown.
Here is my code:
#selenium imports
from re import search
import time
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
PATH = "C:\Program Files (x86)/chromedriver.exe"
driver = webdriver.Chrome(PATH)
what_search = "card"
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
#searches for what_search
print(driver.title)
search = driver.find_element_by_name("Ntt")
search.send_keys(what_search)
search.send_keys(Keys.RETURN)
#this opens up the dropdown menu
store = driver.find_element_by_id("Change-Store")
store.click()
time.sleep(1)
#these are my "attempts" to open up and click on a store location option
#storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
#storeclick = driver.find_element_by_class_name("dropdown-item")
storeclick = driver.find_elements_by_class_name("dropdown-itemLI store_055")
print(storeclick)
storeclick.click()
print("I TRIED")
The main issue is:
storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
does not work to find the dropdown menu button
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Thus I cannot click on it.
I want to click on the dropdown menu, then select a store option:
Using only class name can be tricky to find if the webpage is too big. Try using xpath instead so you can use more filters. Here is an example that can find the button:
Element:
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Xpath: //li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]
storeclick = drive.find_elements_by_xpath('//li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]')
To click on the element with text as MI - Madison Heights you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Code Block:
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-light.accept"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "MI - Madison Heights"))).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
Browser Snapshot:

How to enter login credentials on form using Selenium

I'm having difficulty sending login credentials to a form by selecting the 'class' elements. I get the 'Unable to locate element' error when running the code below. Not sure what to look for in the HTML code
'''
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r"C:\Users\aigag\Documents\Python Projects\chromedriver.exe")
driver.get("https://www.goodlifefitness.com/home.html")
login = driver.find_element_by_class_name("c-header__login-text")
login.click()
time.sleep(1)
driver.switch_to_frame('destination_publishing_iframe_goodlifefitness_0')
loginemail = driver.find_element_by_class_name('c-field__input js-login-email c-login-block__input')
loginpass = driver.find_element_by_class_name("c-field__input js-login-password c-login-block__input")
time.sleep(2)
loginemail.send_keys('#gmail.com')
time.sleep(5)
driver.close()
'''
HTML code
'''
<input type="email" placeholder="Enter Member ID, barcode or email address" class="c-field__input js-login-email c-login-block__input">
'''
Replace spaces with dots. As well as add webdriver waits for page loading and remove time.sleep(). As well as removing the iframe since it wasn't inside.
driver.get("https://www.goodlifefitness.com/home.html")
wait = WebDriverWait(driver, 10)
login = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "c-header__login-text")))
login.click()
loginemail = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "c-field__input.js-login-email.c-login-block__input")))
loginpass = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "c-field__input.js-login-password.c-login-block__input")))
loginemail.send_keys('#gmail.com')
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
If you want to access an element with more than one class name, put a period, not a space, between the class names.
For example:
loginemail = browser.find_element_by_class_name('c-field__input.js-login-email.c-login-block__input')

Selenium Python get_element by ID failing

Can someone help me understand why my code fails to find the element by ID. Code below:
from selenium import webdriver
driver=webdriver.Firefox()
driver.get('https://app.waitwhile.com/checkin/lltest3/user')
element = driver.find_element_by_id("guestPhone")
Inspecting element shows the ID clearly.
<input type="tel" name="guestPhone" id="guestPhone" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-phone-validator ng-invalid-required ng-touched" ng-model="form.model" ng-model-options="{ 'updateOn': 'default blur', 'debounce': { 'default': 350, 'blur': 0 } }" uib-typeahead="guest.phone for guest in form.onChange({value:$viewValue})" typeahead-min-length="6" typeahead-on-select="form.onSelect({guest:$item})" typeahead-select-on-exact="true" uib-tooltip="Please enter valid number. Include country code for non-US numbers" tooltip-trigger="'none'" tooltip-is-open="(form.guestForm.$submitted || form.guestForm.guestPhone.$touched) && form.guestForm.guestPhone.$invalid" tooltip-placement="bottom" ng-required="::form.required" phone-validator="US" placeholder="Mobile phone" title="Please enter a valid phone number" autocomplete="nope" next-on-enter="" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-47-2884" required="required" style="">
P.S. I've also tried XPath and name as well. Still no luck.
You need to wait for the element to become visible on the page. You can tell this is loaded in dynamically because if you right-click on the page in chrome and view source you'll see there's no guestPhone element. It gets loaded in with javascript
Here's an example from http://isaacviel.name/make-web-driver-wait-element-become-visiable/:
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
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
You can try with web driver wait :
Code :
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
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://app.waitwhile.com/checkin/lltest3/user")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'guestPhone')))
element.send_keys('006867987')

click on anchor tag having href = '#'

I am using below python code using selenium. click is not working on anchor tag having href = "#"
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("E:\chromedriver.exe")
driver.get('file:///E:/Selenium/validateTest.html')
driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']").click()
Here is the web html code that I am using.
<h1>Anchor tag</h1>
Show content
<i class="fa fa-binoculars" aria-hidden="true"></i> Validate Data
As per the HTML you have shared it seems the AUT is based on JavaScript, so to click on the link with text as Validate Data you have to induce WebDriverWait for the element to be clickable and you can use either of the following options :
LINK_TEXT :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Validate Data"))).click()
CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-red#validateData"))).click()
XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-red' and #id='validateData']"))).click()
Note : You will require 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
Try using a javascript executor to click your element.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement elementToClick = driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']");
js.executeScript("arguments[0].click();", elementToClick);
Above code needs to be adapted to python ( which i'm not familiar with, but you get the idea)
Hope this helps

Categories

Resources