Python (Selenium) Selecting a drop down list from HHPRED - python

I'm attempting to select drop down menu items from the HHPRED website. (URL: https://toolkit.tuebingen.mpg.de/tools/hhpred) and I keep bumping into either 'object not found,' or 'object not clickable/selectable'.
# Input protein from txt file (predator_file variable)
text_area = driver.find_element_by_id('__BVID__121')
text_area.send_keys(predator_file)
# Input PDB, SCOP, PFAM, and NCBI domains
first_click = driver.find_element_by_id('__BVID__130')
scop_click = driver.find_element_by_link_text("SCOPe")
pfam_click = driver.find_element_by_link_text("Pfam")
ncbi_click = driver.find_element_by_link_text("ncbi_")
I know I'm working on selenium correctly because the first portion for my text entry is copying correctly but, when I'm working on the drop down from selecting it to even picking what I need - I'm lost. See below the inspected elements for HHPRED and the drop down I'm looking at tackling.
Any help would be greatly appreciated!

Currently your url is unreachable due tooc credntials. You can use below code to select value/visible text from the dropdown.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
select= WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "select element xpath")))
print(len(select.options))
select.select_by_value("") # select by value
select.select_by_visible_text('') # select by visible text
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
or
driver.find_element_by_xpath("//select[#name='element_name']/option[text()='option_text']").click()
Updated solution : its a custom dropdown element so you need to handle it in a differnt way . Kindly find below code for your reference. I have verified it and its working as expected .
driver.get("https://toolkit.tuebingen.mpg.de/tools/hhpred")
main_window = driver.current_window_handle
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn sign-in-link btn-href btn-sm']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "(//fieldset[#class='form-group']//input)[2]"))).send_keys('')
wait.until(EC.element_to_be_clickable((By.XPATH, "(//fieldset[#class='form-group']//input)[3]"))).send_keys('')
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-secondary']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Got it!')]"))).click()
print wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Paste Example')]"))).text
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
clickElement=wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='multiselect__tags']")))
ActionChains(driver).move_to_element(clickElement).click().perform()
wait.until(EC.element_to_be_clickable((By.XPATH, "//li[*]//span[contains(text(),'TIGRFAMs_v15.0')]"))).click()
Output:

Related

Unable to Populate Search Field in Python Selenium

I am trying to search www.oddschecker.com using Python and Selenium (chromedriver) but am struggling to populate the search field with the search term.
The code below clicks the magnifying glass ok but the send_keys statement does not populate the expanded search box.
Can anyone see the issue?
driver.find_element_by_xpath('//*[#id="top-menu"]/li[8]/ul/li[1]/span/span').click()
sleep(5)
driver.find_element_by_xpath('/html/body/div[1]/header/div[1]/div/div[1]/div/ul/li[8]/ul/li[1]/div/div/div/form/fieldset/input').send_keys('Bicep')
xpath is brittle, please use relative xpath. see below.
Use explicit waits.
Close modal pop up may appear sometime, may not appear sometime so better to wrap them inside try and except block.
Code :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("http://www.oddschecker.com/")
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.js-close-class"))).click()
print('Clicked on close button if it appears')
except:
pass
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[starts-with(#data-ng-click,'NavSearchCtrl')]"))).click()
search = wait.until(EC.visibility_of_element_located((By.ID, "search-input")))
search.send_keys('Bicep', Keys.RETURN)
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: Element Click Intercepted while submitting a form

So I'm trying to submit a form but something is either preventing me from accessing the box or I'm using a wrong element but I think I'm using the correct one.
Here is my code:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Close']"))).click()
wait = WebDriverWait(driver, 20)
driver.find_element_by_xpath('//*[#id="home-body"]/div[1]/div/div[1]/form/label').click()
driver.find_element_by_xpath('//*[#id="home-body"]/div[1]/div/div[1]/form/label').send_keys('1818437')
driver.find_element_by_xpath('/html/body/div[3]/div[2]/article/section[2]/div[1]/div/div[1]/form/input[2]').click();
What I'm getting on the output is
ElementClickInterceptedException: Message: element click intercepted:
Element ... is
not clickable at point (553, 728). Other element would receive the
click:
(Session info: chrome=93.0.4577.63)
What might be the issue?
Things to noted down in this scenario :-
When you define an explicit waits wait = WebDriverWait(driver, 20), you can always use wait reference in the scope. you do not need to create again and again in same class.
Try to avoid absolute xpath /html/body/div[3]/div[2]/article/section[2]/div[1]/div/div[1]/form/input[2], try with relative xpath/xpath axes.
When we try to send keys to some element, in general it should be a input tag, not label
You may have to scroll, may be not in this case but when you scroll manually to interact with elements in UI, same has to automated with Selenium as well.
Also I observed to this webapp that search and input tags are duplicated, so I have used xpath indexing [2] to handle.
Sample code :-
driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe')
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Close']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[#name='MCSearch'])[2]"))).send_keys('1818437')
wait.until(EC.element_to_be_clickable((By.XPATH, "(//input[#name='search'])[2]"))).click()
You can use below xpaths too.
driver.find_element_by_xpath("//div[#class='sms-search-box']//input[1]").send_keys('1818437')
driver.find_element_by_xpath("//div[#class='sms-search-box']//input[2]").click()
Xpath you are using is not right. Your xpath for the input field should be like this.
driver.find_element_by_xpath("//input[#name='MCSearch' and #placeholder='Type Name or U.S. DOT#']").send_keys("1818437")
driver.find_element_by_xpath("//input[#placeholder='Type Name or U.S. DOT#']//following::input[#value='Search']").click();

How to select a list in Selenium?

I'm trying to enter an address then they propose me some addresses and I had no idea how to select the first option they give me.
If you want to try, at the second step on this link: https://www.sneakql.com/en-GB/launch/culturekings/womens-air-jordan-1-high-og-court-purple-au/register
adresse = chrome.find_element_by_id('address-autocomplete')
adresse.send_keys(row['Adresse']) #Adress from a file
time.sleep(5)
country = chrome.find_element_by_xpath('//li[#id="suggestion_0"]').click();
Inspect element:
Try clicking on the first option with this:
driver.find_element_by_xpath('//li[#id="suggestion_0"]')
UPD
The element you trying to click is out of the view. You have to do the following:
from selenium.webdriver.common.action_chains import ActionChains
suggestion_0 = driver.find_element_by_xpath('//li[#id="suggestion_0"]')
actions = ActionChains(driver)
actions.move_to_element(suggestion_0).perform()
suggestion_0.click()
You should click this field and wait for the first option to become clickable.
I've wrote some code to test if my solution works and it works in all cases for me:
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
url = 'https://www.sneakql.com/en-GB/launch/culturekings/womens-air-jordan-1-high-og-court-purple-au/register'
driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get(url)
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'AGREE')]"))).click() # ACCEPT COOKIES
# Making inputs of the first page
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#firstName"))).send_keys("test")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#lastName"))).send_keys("Last name")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#preferredName"))).send_keys("Mr. President")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#email"))).send_keys("mr.president#gmail.com")
driver.find_element_by_css_selector("#password").send_keys("11111111")
driver.find_element_by_css_selector("#phone").send_keys("222334413")
driver.find_element_by_css_selector("#birthdate").send_keys("2000-06-11")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Next')]"))).click()
# Second page and answer to your main question
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#address-autocomplete"))).send_keys("street")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#suggestion_0"))).click()
Please note, that not all explicit waits are required and I used css selectors because I am not sure that all elements ids are correct.
My output:

How to click buttons with Python Selenium webdriver on this website when the element cannot be found?

I'm very new to Selenium with Python and I'm simply trying to click 2 consecutive buttons on this website: https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28
I want to click the settings icon in the top right corner and then click the celsius option to convert temperatures on the site.
I can achieve this whilst debugging the code and doing the steps one at a time, but when I run the code normally, it is unable to find the second element with the error:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="wuSettings-quick"]/div/a[2]"}
(Session info: chrome=85.0.4183.83)
I have tried identifying the elements by id and xpath as follows:
Button 1: driver.find_element_by_id('wuSettings').click()
Button 2: driver.find_element_by_xpath('//*[#id="wuSettings-quick"]/div/a[2]').click()
Hoping it's an easy fix. All help appreciated. Thanks.
Code below:
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)
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
driver.implicitly_wait(15)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'wuSettings'))
)
element.click()
# element = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.XPATH, '//*[#id="wuSettings-quick"]/div/a[2]'))
# )
# element.click()
except:
driver.quit()
Your code works fine if I replace this line:
driver.implicitly_wait(15)
with
time.sleep(5)
When I need to await for an element to be clickable, I usually have the best luck with something like this
can_click=False
while not can_click:
try:
element=driver.find_element_by_id('wuSettings')
element.click()
# note that if the issue is that the element is not visible in
# the viewport, you may need to do
# driver.execute_script("arguments[0].click();", element)
# instead
can_click=True
except:
pass
basically it should throw an exception until you can click on the element you want and once it does the loop terminates.
First of all you should not use both Implicit and explicit wait together. A cocktail of it could cause an unpredictable wait times. As you are using explicit wait for both buttons, it should be more than sufficient.
Also use of time.sleep might work in this case but its never been a good way to wait in test script as it makes test brittle and unpredictable.
As you can see even after page load few ajax components (Record for day passed in url is loading) are still loading and even though setting element is present its not giving option to change temp, unit record is fully loaded. So explicit / implicit wait on setting icon is not working.
Try below code and it should work.
try:
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, "//div[#class='legend-def temperature']"))) # To insure record for the day is loaded
ele = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, "wuSettings")))
driver.execute_script("arguments[0].scrollIntoView();", ele)
ele.click() #Scroll to Setting icon
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[#title='Switch to Metric']"))).click()
except:
driver.quit()
I tried different things, but it looks like you have to agree cookies use before you can click on another button.
I tried Matt's solution but I'm pretty sure that the commercial loading on top of the page screws up the first click on the settings button.
This is working fine for me :
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
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.wunderground.com/history/daily/gb/manchester/EGCC/date/2017-8-28")
cookies = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//button[#id="truste-consent-button"][#type="button"]'))
)
cookies.click()
time.sleep(5)
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//button[#id="wuSettings"][#class="wu-settings"]'))
)
element.click()
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[#id="wuSettings-quick"]/div/a[2]'))
)
element.click()

Python Selenium Select: "Element <option> could not be scrolled into view"

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time
url = "https://www.bungol.ca/"
driver = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver')
driver.get(url)
#myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
searchbutt = """/html/body/section/div[2]/div/div[1]/form/div/button""" #click search to get to map
active_listing = """//*[#id="activeListings"]"""
search_wait = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, searchbutt)))
search_wait.click()
active_wait = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, active_listing)))
active_wait.click()
driver.find_element_by_xpath("""//*[#id="useDateRange"]""").click() #use data range
time.sleep(1)
driver.find_element_by_xpath("""//*[#id="dateRangeStart"]""").click() #start range
time.sleep(1)
month_path = driver.find_element_by_xpath("""/html/body/div[17]/div/div/div[1]/select""") #click the month to bring combo box list option
driver.execute_script("arguments[0].click();", month_path)
I am trying to select January 1, 2015 on this calendar which requires you to click:
https://www.bungol.ca/map/?
use date range (no problem doing this)
click the start range (no problem)
click the month which brings up a combo form of options (can't click)
click year - can't click
click the date - can't click
I tried:
locate the element by xpath and css path but neither method works.
move_to_element method but still doesn't work
switch to frame method - doesn't work because it's not inside an iframe
use javascript to click it found here: How do you click on an element which is hidden using Selenium WebDriver?
scroll to element - doesn't do anything because element is already on screen
Here is a code block to solve your issue, I reorganized some of your code as well. Please make sure to import this from selenium.webdriver.support.ui import Select so you can use the Select in the code block:
driver.get('https://www.bungol.ca/')
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, './/button[#type="submit" and text()="Search"]'))).click()
wait.until(EC.element_to_be_clickable((By.ID, 'activeListings'))).click()
wait.until(EC.element_to_be_clickable((By.ID, 'useDateRange'))).click()
# I found that I had to click the start date every time I wanted to interact with
# anything related to the date selection div/table
wait.until(EC.element_to_be_clickable((By.XPATH, './/input[#id="dateRangeStart" and #name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[#id="dateRangeStart" and #name="soldDateStart"]').click()
yearSelect = Select(driver.find_element_by_xpath('.//select[#class="pika-select pika-select-year"]'))
yearSelect.select_by_visible_text('2015')
wait.until(EC.element_to_be_clickable((By.XPATH, './/input[#id="dateRangeStart" and #name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[#id="dateRangeStart" and #name="soldDateStart"]').click()
monthSelect = Select(driver.find_element_by_xpath('.//select[#class="pika-select pika-select-month"]'))
monthSelect.select_by_visible_text('January')
wait.until(EC.element_to_be_clickable((By.XPATH, './/input[#id="dateRangeStart" and #name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[#id="dateRangeStart" and #name="soldDateStart"]').click()
driver.find_element_by_xpath('.//td[#data-day="1"]').click()
After running that, you should have the date selected January 1, 2015 for the first part of the range. You can use the same techniques to select the second part of the range if needed.
For more information on how to use the Select please visit THIS.

Categories

Resources