How to display the count of dropdown items using selenium and python? - python-3.x

I am trying to get all elements within a drop down and print the total count. Here's the code, which I have written for this.
import unittest
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
class dropdown(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
def test_selectmethod(self):
driver=self.driver
driver.get("http://magento-demo.lexiconn.com/")
driver.find_element_by_xpath("//*[#id='select-language']").click()
dropdown=Select(driver.find_element_by_xpath("//*[#id='select-language']"))
print str(len(dropdown)+ "products found"
for i in dropdown:
print(i.text)
def tearDown(self):
self.driver.close()
However, this throws error while printing str(len(dropdown).

Try str(len(dropdown.options)).

To display the count of dropdown items using selenium python you don't even have to identify the <select> tag and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://magento-demo.lexiconn.com/")
count = len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//select[#id='select-language']//option"))))
print(str(count) + " items found." )
Console Output:
3 items found.
But if you want to print the <options> within the <select> tag you have to identify the <select> tag and you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("http://magento-demo.lexiconn.com/")
count = len(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//select[#id='select-language']//option"))))
print(str(count) + " items found." )
dropdown = Select(driver.find_element_by_xpath("//*[#id='select-language']"))
print("Items are: " )
for i in dropdown.options:
print(i.get_attribute('innerHTML'))
Console Output:
3 items found.
Items are:
English
French
German

Related

scroll google map webpage sidebar using selenium and python

I am trying to collect data on a google map webpage, this is the link. link
This is the code I have tried. My idea is to scroll to the "website name" (you can find the website name once you scroll down) once is present in the browser. but it is not scrolling.
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
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("https://www.google.com/maps/place/Amsterdamsche+Athleten+Club+Hercules/#52.36937,4.8049968,16.25z/data=!4m5!3m4!1s0x47c5e3c9cbb3d913:0xef85f93ef996cc06!8m2!3d52.3692292!4d4.8056684")
img_result = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="QA0Szd"]/div/div/div[1]/div[2]/div/div[1]/div/div/div[7]/div[5]/a/div[1]/div[2]/div[1]')))
driver.execute_script("arguments[0].scrollIntoView(true);",img_result)
print(img_result.text)
driver.close()
what is the solution for this?
EDIT:This is what I'm trying to get.
At least on my side I see no need to scroll.
The following code worked:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://www.google.com/maps/place/Amsterdamsche+Athleten+Club+Hercules/#52.36937,4.8049968,16z/data=!4m5!3m4!1s0x47c5e3c9cbb3d913:0xef85f93ef996cc06!8m2!3d52.3692292!4d4.8056684"
driver.get(url)
name = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-item-id='authority']"))).text
print(name)
Output:
aachercules.nl
The same could be done with XPath instead of CSS Selectors.
This is the XPath I used:
name = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[#data-item-id='authority']"))).text

How to make a webpage stop loading and extract text from it

I want to extract the text from a url-shortner using this code :
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
os.environ['PATH'] += 'C:/Selenium Drivers'
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://pastebin.com/vkuagfwV')
strings = str(driver.find_element(By.CLASS_NAME, 'textarea').text)
strings = strings.replace("\n", " ")
driver.close()
print(strings)
But this code is not working until I manually stop the web-page from stop loading. I tried using XPATH as well but it didn't work.
Instead of implicitly_wait try using Expected Conditions visibility_of_element_located method here.
Also as mentioned in comments you don't need to use str casting there.
Please try this:
import os
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
os.environ['PATH'] += 'C:/Selenium Drivers'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get('https://pastebin.com/vkuagfwV')
strings = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "textarea"))).text
strings = strings.replace("\n", " ")
driver.close()
print(strings)
UPD
Please add eager pageLoadStrategy configuration.
import os
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
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
os.environ['PATH'] += 'C:/Selenium Drivers'
caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "eager"
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\path\to\chromedriver.exe')
#driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get('https://pastebin.com/vkuagfwV')
strings = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "textarea"))).text
strings = strings.replace("\n", " ")
driver.close()
print(strings)

Click on show more button; selenium scrape with python

I'm trying to scrape a website with show more button; and I'm not able to click on it.
The website is: https://www.wtatennis.com/rankings/singles
And my code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains
from tqdm import tqdm
import time
options = Options()
options.add_argument("--headless")
browser = webdriver.Chrome(ChromeDriverManager().install(),options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
action = ActionChains(browser)
showmore = browser.find_elements_by_xpath(".//button[contains(#class, 'btn widget-footer__more-button rankings__show-more js-show-more-button')]")
action.move_to_element(showmore).perform()
showmore.click()
time.sleep(5)
Has anyone any idea? Thanks!
Don't use './/' in your locator when you are starting the search from root, as there is no current element your locator won't find any element. Also you can use any attribute to find elements uniquely. see below code:
browser = webdriver.Chrome(options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
use webdriver wait and data attributes
tu use wait import:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
To wait till all elements are loaded you have to make sure last element is not changing , if its changing keep scrolling .
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text="Accept Cookies"]'))).click()
value = "start"
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,
'//*[#data-text = "Show More"]'))).click()
while(browser.find_element_by_xpath("(//tr[#class='rankings__row'])[last()]").text != value):
elem = browser.find_element_by_xpath(
'(//*[contains(text(),"Loading")])[2]')
value = browser.find_element_by_xpath(
"(//tr[#class='rankings__row'])[last()]").text
browser.execute_script("arguments[0].scrollIntoView()", elem)
WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,
"//tr[#class='rankings__row']")))
try:
WebDriverWait(browser, 10).until_not(EC.text_to_be_present_in_element((By.XPATH,
"(//tr[#class='rankings__row'])[last()]"), value))
except:
None

selenium - Not able to find input

Trying to create script which will subscribe to news automatically, but stuck with a problem, selenium not able to find email input and submit button. Everytime getting selenium.common.exceptions.NoSuchElementException:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
path_to_chromedriver = 'chromedriver'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=path_to_chromedriver)
driver.get('https://dataengweekly.com/')
driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)
email_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'input[type="email"]'))
)
email_input.send_keys("email#test.com")
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(10)
Note - Your subscription textbox is in a different iframe, To work with that iframe you need to first switch to that iframe.
Try below code and let me know if you need more clarification -
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
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.webdriver import ActionChains
import time
chrome_options = Options()
chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)
driver.get('https://dataengweekly.com/')
iframe = driver.find_element_by_xpath('//iframe')
driver.switch_to.frame(iframe)
email_input = wait.until(EC.presence_of_element_located((By.XPATH, "//input[#type='email']")))
action.move_to_element(email_input).click().send_keys("email#test.com").perform()
driver.find_element_by_css_selector('button.subscribe-btn').click()
time.sleep(2)

Can't get this selenium web driver to wait. but I can get it to sleep why?

Been struggling on this for a little while now and I'm pulling my hairout. Why does the 1st code not work? But second one does?
'''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
import time
driver = webdriver.Chrome(executable_path='/Users/jackrossanderson/Desktop/chromedriver')
driver.get('https://www.url.Iwanttoscrape.com')
search = driver.find_element_by_name('searchbar')
search.send_keys("hometown")
search.send_keys(Keys.RETURN)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.Class_Name, 'button i want to click'))
)
element.click()
driver.back()
except:
print(error)'''
'''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
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path='/Users/jackrossanderson/Desktop/chromedriver')
driver.get('https://www.urlIwanttoscrape')
search = driver.find_element_by_name('searchbar')
search.send_keys("home town")
search.send_keys(Keys.RETURN)
time.sleep(12)
element = driver.find_element_by_class_name('button I want to press')
element.click()
driver.back()'''
I know the second way is frowned upon but I cant see why the top one doesnt work?
You can try using
element = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CLASS_NAME, "button i want to click"))) to make the driver wait and click based on the class name
Use, element_to_be_clickable is recommended as mentioned here because, presence_of_element_located description defines that it does not necessarily mean the element is visible or clickable.
Also, check the casing for By.CLASS_NAME used.

Resources