Conditional selection of elements in Selenium webdriver in Python - python-3.x

I'm facing a situation where a modal sometimes has a desired button for logging into Facebook and sometimes I have to click 'More Options' to get to the Facebook button. Another hurdle is that the xpath of the more_options_btn is the same as one called 'Trouble logging in?' so I added a [contains(text(),"More Options")], but am unsure of the syntax here and cannot find the right approach in the docs. In any case it is throwing an error, which might be caused by the fact that the button element does not have text, instead it is nested like this button > span > text
Please keep in mind that I want to make my solution as dynamic and robust as possible, but don't have a lot of experience with Selenium yet.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://localhost:3000/')
try:
fb_btn = driver.find_element_by_xpath(
'//*[#id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button')
fb_btn.click()
except:
more_options_btn = driver.find_element_by_xpath(
'//*[#id="modal-manager"]/div/div/div/div/div[3]/span/button[contains(text(),"More Options")]')
more_options_btn.click()
fb_btn = driver.find_element_by_xpath(
'//*[#id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button')
fb_btn.click()

Try to change xpath to:
//*[#id="modal-manager"]/div/div/div/div/div[3]/span/button[contains(.,"More Options")]
It can find the children node of 'button' contains has the text.

You can use OR operator in xpath | to match your xpath with multiple condition. I would recommend to use Relative xpath instead of Absolute one.
As you xpath are too lengthy so it would look like this with OR condition
//*[#id="modal-manager"]/div/div/div/div/div[3]/span/div[2]/button | //*[#id="modal-manager"]/div/div/div/div/div[3]/span/button[contains(text(),"More Options")]

With a combination from the input above I managed to get it to work. I got rid of a lot of the long xpath. This has the advantage that my code will still work if the order of the buttons change. It will however still break if the text of the buttons changes, so it is a compromise. Here is the result:
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://localhost:3000/')
sleep(5)
try:
fb_btn = driver.find_element_by_xpath(
"//*/button[contains(.,'Log in with Facebook')]").click()
except NoSuchElementException:
more_options_btn = driver.find_element_by_xpath(
"//*[contains(text(), 'More Options')]")
more_options_btn.click()
fb_btn = driver.find_element_by_xpath(
'//*/button[contains(.,"Log in with Facebook")]')
fb_btn.click()

Related

Selenium driver - not able to close the popup, element not found is the main issue

Using selenium find element (using xpath) method to close the popup but it is not able to detect it.
time.sleep(10)
driver.fin_element(By.XPATH,"XPATH").close()
I have also use time.sleep and webdriver wait methods but it not working
Website: www.multcloud.com
time.sleep(10)
webdriver wait
ec. Element traceable method
Also try Find elements but it is showing empty list.
Tried find element using class_name,xpath, full xpath,cs locator,link text
Try the below code
driver.get("https://www.multcloud.com/")
sleep(4)
driver.switch_to.frame("layui-layer-iframe1")
You could use explicit wait in the place of sleep
sleep(3)
button = driver.find_element(By.XPATH,"((//div[#class='sale-close-img'])[2])")
button.click()
Imports
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

SeleniumPython3.x How can i handle this element?

I want to enter text in this element and want to choose the suggested element that is coming up. send_keys("text") is not working with this element and throwing exception "ElementNotInteractableException". Can anyone please suggest how can i handle this element?
Code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://admin-demo.nopcommerce.com/Admin/Customer/Create")
driver.find_element(By.XPATH, "//button[#type='submit']").click()
time.sleep(2)
#now when i am sending keys to below element it is throwing the exception:
driver.find_element(By.XPATH, "(//div[#class='k-multiselect-wrap k-floatwrap'])[1]").send_keys("your")
i am not able to proceed further with this as it throws exception. Please suggest how can i enter the text in it and the suggestion option that comes with it, how can i select one of it? Thanks.
You need to send the text to input element inside that div instead of the div itself.
(//div[#class='k-multiselect-wrap k-floatwrap'])[1]//input

Unable to click on one of the items in a drop down list using selenium (python)

I am unable to click on the 'Search photos' button on flickr (image below including the html).
I have tried the following:
sp = browser.find_element_by_partial_link_text('/search/?text=tennis%20shoes')
sp.click()
sp = browser.find_element_by_name('Select photos')
sp.click()
searchPhotos = browser.find_element_by_class_name('Search photos')
searchPhotos.click()
browser.find_element_by_xpath("//class[#name='Search photos']").click()
But none of them seem to work. I am learning how to do this, including how to use xpath, so maybe I am not using it correctly. Any advice to point me in the right direction?
EDIT: full section of code to answer comment below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", '/Users/home/Box/Temp-to delete')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'png/jpg')
browser = webdriver.Firefox(firefox_profile=profile, executable_path='/usr/local/bin/geckodriver')
browser.get('https://www.flickr.com/')
searchBar = browser.find_element_by_css_selector('#search-field')
searchBar.send_keys(searchTerm)
browser.find_element_by_xpath(".//*[#data-track='autosuggestNavigate_searchPhotos']").click()
Using firefox 72.0.2 (64-bit), python3, geckodriver v0.26.0
The path used in your XPath won't work. Try this one .//*[#data-track='autosuggestNavigate_searchPhotos'].
The .// tells Selenium so search anywhere in the DOM. The asterisk (*) will make Selenium to look for any element (no matter if it is div, li or any other HTML tag). Then it will check which element has the data-track attribute, with value autosuggestNavigate_searchPhotos. Since there is only one element like this, we are fine.
I advise to read more about XPath and train a bit, you may start here
Solved it Just had to hit ENTER for the photos results page to show. Here is the single line of code I changed:
searchBar.send_keys(searchTerm, Keys.ENTER)

ElementNotVisibleException: Message: element not interactable error while trying to click on the top video in a youtube search

I cannot seem to find a way to click on the right element in order to get the url I am looking for. In essence I am trying to click on the top video in a youtube search (the most highly ranked returned video).
How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver?
-> This is for Java but it let me in the right direction (knowing I needed to execute JavaScript)
http://www.teachmeselenium.com/2018/04/17/python-selenium-interacting-with-the-browser-executing-javascript-through-javascriptexecutor/
-> This shows me how I should try to execute the javascript in python.
I have also seen countless articles about waits but they do not solve my problem.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
wrds = ["Vivaldi four seasons", "The Beatles twist and shout", "50
cent heat"] #Random list of songs
driver = webdriver.Chrome()
for i in wrds:
driver.get("http://www.youtube.com")
elem = driver.find_element_by_id("search")
elem.send_keys(i)
elem.send_keys(Keys.RETURN)
time.sleep(5)
driver.execute_script("arguments[0].click()",driver.find_element_by_id('video-title')) #THIS CLICKS ON WRONG VIDEO
#elem = driver.find_element_by_id("video-title").click() #THIS FAILS
time.sleep(5)
url = driver.current_url
driver.close()
I get a ElementNotVisibleException: Message: element not interactable error when I do not execute any javascript (even though it has actually worked before it is just no way near robust). When I do execute the javascript it clicks on the wrong videos.
I have tried all types of waits "Explicit" and "Implicit" this did now work.
I am quite sure I need to execute some JavaScript but I don't know how.
You were almost there. You need to induce WebDriverWait for the element to be clickable and you can use the following solution:
Code Block:
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
wrds = ["Vivaldi four seasons", "The Beatles twist and shout", "50 cent heat"]
kwrd = ["Vivaldi", "Beatles", "50"]
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\\chromedriver.exe')
for i, j in zip(wrds, kwrd):
driver.get("https://www.youtube.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys(i)
driver.find_element_by_css_selector("button.style-scope.ytd-searchbox#search-icon-legacy").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.title-and-badge.style-scope.ytd-video-renderer a"))).click()
WebDriverWait(driver, 10).until(EC.title_contains(j))
print(driver.current_url)
driver.quit()
That's one of the reasons you should never use JavaScript click, Selenium Webdriver is designed to stimulate as if a real user is able to click. Real user can't click an invisible element in the page but you can click through Javascript. If you search the element by that id video-title, it matches totally 53 videos. But I don't know which one you want to click. You may match that element by some other way(not by id).
I will give you an idea how to click that element but you need to find out the index first before you click.
driver.find_element_by_xpath("(//*[#id='video-title'])[1]").click
If the first one is invisible, then pass 2, [2] then three, figure out which one it's clicking the desired element. Or you may specify the exact element, we may try to locate that element by some other way.

Selenium NoSuchElement Exception with specific website

I'm recently trying to learn Selenium and found a website that just ignores my attempts to find particular element by ID, name or xpath. The website is here:
https://www.creditview.pl/PL/Creditview.htm
I am trying to select first text window, the one labeled Uzytkownik, the code for it goes like that:
I am trying to find it using several methods:
from selenium import webdriver
browser = webdriver.Chrome()
site = "https://www.creditview.pl/pl/creditview.htm"
browser.get(site)
login_txt = browser.find_element_by_xpath(r"/html//input[#id='ud_username']")
login_txt2 = browser.find_element_by_id("ud_username")
login_txt3 = browser.find_element_by_name("ud_username")
No matter what I try I keep getting:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
as if the element wasn't there at all.
I have suspected that the little frame containing the field might be an iframe and tried to switch to various elements with no luck. Also tried to check if the element isn't somehow obscured to my code (hidden element). Nothing seems to work, or I am making some newbie mistake and the answer is right in front of me. Finally I was able to select other element on the site and used several TAB keys to move cursor to desired position, but is feels like cheating.
Can someone please point show me how to find the element ? I literally can't sleep because of this issue :)
Given that your element is there, you still need to wait for your element to be loaded/visible/clickable etc. You can do that using selenium's expected conditions (EC).
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
my_XPATH = r"/html//input[#id='ud_username']"
wait_time = 10 # Define maximum time to wait in seconds
driver = webdriver.Chrome()
site = "https://www.creditview.pl/pl/creditview.htm"
driver.get(site)
try:
my_element = driver.WebDriverWait(driver, wait_time).until(EC.presence_of_element_located(By.XPATH,my_XPATH))
except:
print ("element not found after %d seconds" % (wait_time))

Resources