I would like to click on the element "Project" to display the dropdown list (see image below)
Using the selenium library in python elsewhere i get the error :
could not be scrolled into view
which is obtained, for instance, using this code:
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_xpath('//div[#class="multiselect-container"]').click()
or using some code where I wait the element to be displayed, for instance like described here:
Message: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium
mySelectElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "edit-projects")))
mySelectElement.click()
But I can't get it working.
Any would be appreciated.
The html source code may be found here:
https://rpidejr.hopto.org/f/8a909b51dcc34d09a00a/
I saved the source code file to my drive as test.html and opened it and click on the Project box with the below code. Just edit the paths to your local machine.
from selenium import webdriver
import time
driver = webdriver.Firefox(executable_path=r'C:\\Path\\To\\geckodriver.exe')
driver.get("file:///C:/Path/To/test.html")
time.sleep(1)
#project = driver.find_element_by_xpath("//select[#id='edit-projects']")
#project.click()
project_elements = driver.find_elements_by_xpath("//select[#id='edit-projects']")
for element in project_elements:
try:
element.click()
except Exception as e:
print(e)
Related
I'm new to webdrivers and am experimenting with them.
I'm trying to click a button when opening a webpage and it keeps giving the error of unable to locate element.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("html page")
button = driver.find_element(By.ID, "onetrust-accept-btn-handler")
button.click()
i have tried id and xpath but i dont know what else to use.
the path for the button is:
/html/body/div[3]/div[3]/div/div/div[2]/div/div/button
<button id="onetrust-accept-btn-handler" tabindex="0">Run</button>
You can use implicit wait for wait until the page is fully loaded
driver.implicitly_wait(10)
This question already has answers here:
Click on ember.js enabled element using Selenium
(5 answers)
Closed 1 year ago.
At this stage, all the script aims to do is to locate the search bar in a digital library, send the name of the resource I am looking for, click on it and get the current url. The first bit (sending keys to search bar) works fine. But I have noticed that the second try/finally block only executes when I right click and inspect any element on the page. If I don't, I get the following error message :
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="ember767"]"}
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
PATH = "/Applications/chromedriver"
ser = Service(PATH)
driver = webdriver.Chrome(service=ser)
driver.maximize_window()
driver.implicitly_wait(20)
driver.get("https://browzine.com/libraries/1374/subjects")
print("Enter targeted Journal name:")
targeted_journal = input()
wait = WebDriverWait(driver, 10)
try:
button = driver.find_element(By.ID, "ember648")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
button.send_keys(targeted_journal)
button.send_keys(Keys.RETURN)
finally:
pass
# step 2 : click targeted journal and get current url
try:
driver.implicitly_wait(20)
click_journal = driver.find_element(By.ID, "ember767")
ActionChains(driver).move_to_element(click_journal).click(click_journal).perform()
targeted_url = driver.current_url
finally:
pass
print(targeted_url)
driver.quit()
Update - answer found
The ID initially collected for the targeted element was a dynamic one. It was only as such when the developer tools window was open. Therefore the script was only able to detect that particular ID when "Inspect element" was clicked as it was the only case in which the targeted element was assigned this particular ID ("ember767" in my case).
To avoid this issue, an Xpath was used to locate the element instead. See Mayank Shukla's answer below.
In some cases, element's ID or other attributes changes or attribute's value appears on mouse hover. So it's preferred to use xpath for locating such elements
In this scenario, I think the element's id is changing after you are hovering the mouse. Try using xpath and let me know if that works. Because might be after browser refresh this is happening i.e. element's id is changing.
I am trying to build a Web Scraping script using Python (rev. 3.8) and Selenium, Firefox and geckodriver (all latest versions).
I need to select an Item from a dropdown list. My Problem: when I try to select an Item I get the following message:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <option> could not be scrolled into view
My code:
from selenium.webdriver.support.ui import Select
import time
url = 'https://www.hessen-forst.de/marktplatz/#brennholz'
driver = webdriver.Firefox(executable_path=r'D:\\Program Files\\geckodriver-v0.29.0-win64\\geckodriver.exe')
driver.get(url)
time.sleep(5)
print(len(driver.find_elements_by_id("nf-field-166")))
element = driver.find_element_by_id("nf-field-166")
select = Select(driver.find_element_by_name("nf-field-166"))
print(len(select.options))
driver.execute_script("arguments[0].scrollIntoView();", element)
select.select_by_visible_text("Nidda")
the print(len(select.options)) line gives me the correct number of instances I expect. Printing the options names with print(select.obtions[i].text) also gives correct results. Therefor I expect the right drop-down-list was selected.
driver.execute_script("arguments[0].scrollIntoView();", element) brings the drop-down-list in the center of the Browser-Window for a fraction of a second, with select.select_by_visible_text("Nidda") the drop-down-list is slightly below the visible area.
I have already tryed the WebDriverWait: WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='nf-field-166']//options[contains(.,'Nidda')]"))). Here the Result is a Timeout.
Using select_by_value gives the same error.
Can you help me with another idea?
Thanks!
I am learning python and selenium right now by making a small script that posts birthday comments to my facebook friends. I am able to successfully login and navigate to the "friends' birthday" modal, but then I am unable to comment in the textbox.
Here is the error I am getting:
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="u_i_6"]"}
I haven't had any issues finding other page elements via XPath so I am not sure what is causing the issue. I also tried finding the textbox to comment via classname, but had the same result. Can anyone offer some thoughts?
EDIT: forgot to paste code
from selenium import webdriver
from pynput.keyboard import Key, Controller
import time
import config
# fetch facebook password from separate file in the same directory
pw = config.secret['pw']
keyboard = Controller()
driver = webdriver.Chrome()
options = webdriver.ChromeOptions()
options.add_argument('--disable-extensions')
options.add_argument('--disable-notifications')
# navigate to facebook url
driver.get('https://facebook.com')
# input email address
email_input = driver.find_element_by_xpath('//*[#id="email"]')
email_input.send_keys(<omitted>)
# input pw
password_input = driver.find_element_by_xpath('//*[#id="pass"]')
password_input.send_keys(pw)
# click login button element
login_button = driver.find_element_by_xpath('//*[#id="u_0_b"]')
login_button.click()
home_button = driver.find_element_by_xpath('//*[#id="u_0_c"]/a')
# wait 8 seconds for browser popup before pressing esc to get out
time.sleep(8)
keyboard.press(Key.esc)
# check if there are any birthdays today
birthday_section = driver.find_element_by_xpath('//*[#id="home_birthdays"]/div/div/div/div/a/div/div/span/span[1]')
birthday_section.click()
first_comment = driver.find_element_by_xpath('//*[#id="u_i_6"]')
first_comment.send_keys('happy birthday!')
# click post button to post comment
# close browser window after actions are complete
time.sleep(5)
driver.close()
It is hard to answer without the code, but here are a couple of ideas:
Make sure you are using some sort of Selenium wait, implicit or explicit, so your code does not search for an element before this element appears on the page. You can add this code after driver.get() for implicit wait:
driver.implicitly_wait(5)
Also, it looks like IDs are dynamic on that page, I get a different one on my FB page. Try using this xpath to find the textarea:
"//form[contains(#action, 'birthday')]//textarea"
Hope this is helpful, good luck!
I have all of my options selected on this form and have the following code to click the Search button:
WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies"))).click()
I have also tried this:
search = WebDriverWait(wd, 10).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies")))
search.send_keys(Keys.ENTER)
I have also tried subbing in RETURN instead of ENTER.
Here is a screenshot after that line is run:
The search bar is highlighted, but no results are being displayed. I can replicate this screen on my own and am able to press the search button so I don't think it is missing fields.
I believe you are actually clicking the search button but are not seeing the results because they are below the view you show. The scrollable area in your screenshot was a clue.
Clicking the search button is as easy as search.click() after you've executed the selector. But then you need to wait and scroll the page.
To Scroll a page you need to introduceActionChains in the Selenium library and to wait for the load you'll need to use Python's time.sleep(sec) function.
Full code to produce the attached picture:
#Imports
import time
from selenium.webdriver.common.action_chains import ActionChains
#...Code to Load and Select Filters...#
search = WebDriverWait(wd, 3000).until(EC.element_to_be_clickable((By.ID, "alMatchFrequencies")))
search.click()
# Wait for element to load
time.sleep(3)
# Locate element to scroll to
results = WebDriverWait(wd,3000).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/section/div/div[2]/div/div[2]/div[5]/div/div[1]/div[5]")))
actions = ActionChains(wd)
# Scroll
actions.move_to_element(results).perform()
wd.get_screenshot_as_file("test3.png")