Error while using Drop-down - Python Selenium - python-3.x

Hi All
Im trying to select a value from drop-down using class_name/xpath
but it's not working. Tried using id but found that the id keeps
changing. Need assistance on this.
This is the dropdown where im trying to select short description
Below is my 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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
import time
username = '*****'
password = '*****'
url = 'https://*****.service-now.com/'
driver = webdriver.Chrome("C:\WebDrivers\chromedriver.exe")
driver.get(url)
driver.switch_to.frame('gsft_main')
driver.maximize_window()
driver.find_element_by_id('user_name').send_keys(username)
driver.find_element_by_id('user_password').send_keys(password)
driver.find_element_by_id('sysverb_login').click()
wait = WebDriverWait(driver, 10)
incident = wait.until(EC.visibility_of_element_located((By.XPATH, '//span[contains(text(),
"Incident")]')))
incident.click()
open = driver.find_element_by_link_text('Open - Unassigned')
open.click()
time.sleep(25)
element = driver.find_element_by_class_name('form-control default-focus-outline')
dropdown.Select(element)
dropdown.select_by_value('short_description')
Below is the element that I copied and got.
Copy Element:
<select id="d5f43fd407945410af12f2ae7c1ed05c_select" class="form-control default-focus-outline" aria-
expanded="false"><option value="zztextsearchyy" selected="SELECTED" role="option">for text</option>
<option value="number" role="option">Number</option><option value="opened_at"
role="option">Opened</option><option value="short_description" role="option">Short
description</option><option value="caller_id" role="option">Caller</option><option value="priority"
role="option">Priority</option><option value="state" role="option">State</option><option
value="category" role="option">Category</option><option value="assignment_group"
role="option">Assignment group</option><option value="assigned_to" role="option">Assigned to</option>
<option value="sys_updated_on" role="option">Updated</option><option value="sys_updated_by"
role="option">Updated by</option></select>
By xpath:
//*[#id="d5f43fd407945410af12f2ae7c1ed05c_select"]
Gives me the below error:
DevTools listening on ws://127.0.0.1:49915/devtools/browser/30b98d3d-0779-4d85-86bc-9af3a24726a2
[7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
File "Test.py", line 39, in <module>
element = driver.find_element_by_class_name('form-control default-focus-outline')
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in
find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in
find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"css selector","selector":".form-control default-focus-outline"}
(Session info: chrome=81.0.4044.113)
The above message has two different errors:
1) [7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)]
XXX Init()
2) Unable to locate
element: {"method":"css selector","selector":".form-control default-focus-outline"}
Need help on how do I resolve these issues.

Try with WebDriverWait to resolve your issue. If you are still facing issue then check if your element is present within iframe
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//select[#id='form-control default-focus-outline']/option[text()='Short description']")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Working solution :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
chrome_options = webdriver.ChromeOptions()
options = Options()
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(executable_path="C:\New folder\chromedriver.exe",chrome_options=chrome_options)
username = '********'
password = '********'
url = 'https://*******.service-now.com/'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
iframe = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'gsft_main')))
driver.switch_to.frame(iframe)
wait.until(EC.presence_of_element_located((By.ID, "user_name"))).send_keys(username)
wait.until(EC.presence_of_element_located((By.ID, "user_password"))).send_keys(password)
wait.until(EC.presence_of_element_located((By.ID, "sysverb_login"))).click()
driver.switch_to.default_content()
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='filter']"))).send_keys("Incident")
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='sn-widget-list-title ng-binding'][contains(text(),'Open - Unassigned')]"))).click()
iframe = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'gsft_main')))
driver.switch_to.frame(iframe)
print wait.until(EC.element_to_be_clickable((By.XPATH, "//h2[contains(#class,'navbar-title list_title')]"))).text
element=wait.until(EC.element_to_be_clickable((By.XPATH, "//span[#class='input-group-addon input-group-select']//select")))
element=wait.until(EC.presence_of_element_located((By.XPATH, "//span[#class='input-group-addon input-group-select']//select")))
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
select = Select(element)
select.select_by_visible_text("Short description")

Related

Message;no such element(selenium xpath)

I have a problem with selenium find_by_xpath
This is my xpath : //input [#type='text']
And I get this error:
enter image description here
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input [#type='text']"}
from selenium import webdriver
path="C:\Program Files (x86)\chromedriver.exe"
username="123456"
password="123456"
url="https://www.foxesscloud.com/login"
driver=webdriver.Chrome(path)
driver.get(url)
driver.find_element_by_xpath("//input [#type='text']").send_keys("Pies123")
print("done")
print("Log")
It is because of Elements are not rendered properly and you are trying to interact with them. Please use Explicit wait to handle this situation and error.
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.foxesscloud.com/login")
wait.until(EC.visibility_of_element_located((By.XPATH, "//input [#type='text']"))).send_keys('Pies123')
wait.until(EC.visibility_of_element_located((By.XPATH, "//input [#type='password']"))).send_keys('Your password here')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-click"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

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

How to hit the Next button from Python using Selenium?

I am trying to work with the following site using Python.
Please advise how can I hit the Next button:
I have tried the following:
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
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome(executable_path = r'C:/chromedriver.exe')
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get('https://www.okcupid.com/login')
driver.execute_script('arguments[0].click();',wait.until(EC.element_to_be_clickable((By.ID, 'username'))))
wait.until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys("abc")
driver.execute_script('arguments[0].click();',wait.until(EC.element_to_be_clickable((By.ID, 'username'))))
wait.until(EC.element_to_be_clickable((By.ID, 'password'))).send_keys("123")
# Find login button
login_button = driver.find_element_by_name('Next')
# Click login
login_button.click()
But got an error:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="Next"]"}
Name is a an attribute on a webelement. The next button has no Name attribute.
You can try this xpath:
//input[#value='Next']
login_button = driver.find_element_by_xpath('//input[#value="Next"]')

How to click element using selenium web driver(Python)

I am trying to click an element using selenium chromedriver by the Use of ID of an element to click.
I want to Click Year '2020" from the following webpage: 'https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan'
I tried with the below code.
driver = webdriver.Chrome(executable_path=ChromeDriver_Path, options = options)
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
Id = "ctl00_ContentPlaceHolder1_gvMajorIncident_ct123_lbtnYear" ### Id of an Element 2020
wait = WebDriverWait(driver, 20) ##Wait for 20 seconds
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
time.sleep(10)
but unfortunately this gives an Error as below:
element = wait.until(EC.element_to_be_clickable((By.ID, Id)))
File "C:\Users\Pavan\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Please anyone help me on this... Thanks;
I don't know if your imports were correct. Also, your code doesn't scroll to the bottom of the page. Use this.
import os
from selenium.webdriver.support import expected_conditions as EC
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
driver = webdriver.Chrome(executable_path='driver path')
driver.get('https://www.satp.org/datasheet-terrorist-attack/major-incidents/Pakistan')
driver.maximize_window()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
element = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="ctl00_ContentPlaceHolder1_gvMajorIncident_ctl23_lbtnYear"]')))
element.click()

Unable to click on link using python and selenium

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# go to website
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chrome_options)
action = webdriver.ActionChains(driver)
driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')
# look for "login" and click
loginclick = driver.find_element_by_xpath("//*[#id='header']/div[3]/ol/li[3]/a/span").click()
How come I'm not able to click on the login section on top right after navigating to the website given? I get an error:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='header']/div[3]/ol/li[3]/a/span"}
(Session info: chrome=85.0.4183.83)
File "C:\python\download.py", line 18, in <module>
loginclick = driver.find_element_by_xpath("//*[#id='header']/div[3]/ol/li[3]/a/span").click()
Thank you !
Two reason:
1: Wait for element to load
2: As element is not visible on page use java script to click
driver.get('https://www.clinicalkey.com/#!/browse/book/3-s2.0-C2016100010X')
# look for "login" and click
loginclick=WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//*[#id='header']/div[3]/ol/li[3]/a/span")))
driver.execute_script("arguments[0].click();", loginclick)
Output:

Resources