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")
Related
Question:
How can I show all reviews by clicking "Show more reviews" button?
What did I do:
To scrape all reviews, I decided to Keep clicking until that button disappears.
But some new reviews didn't appear after clicking for 8 times (using while below).
I already checked xpath of the button but it didn't changed.
If I click the button manually without the driver, I can see new reviews.
import time, random
from selenium import webdriver
from latest_user_agents import get_random_user_agent
### ser user_agent
user_agent = get_random_user_agent()
options = webdriver.ChromeOptions()
options.add_argument("--user-agent=" + user_agent)
driverpath = "C:/Users/~~/chromedriver.exe"
driver = webdriver.Chrome(chrome_options=options,executable_path=driverpath)
### target url
url = "https://www.sony.co.uk/electronics/truly-wireless/wf-l900/reviews-ratings"
### open URL
driver.get(url)
time.sleep(5)
# accept cookies
driver.find_element_by_xpath('//*[#id="onetrust-accept-btn-handler"]').click()
# show all reviews
try:
while True:
driver.execute_script('window.scroll(0,1000000);')
driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button').click()
time.sleep(random.randrange(2, 5, 1))
except:
print("---------------- finish showing all reviews ----------------")
Try going to the end of the webpage and then do it as the button might not been loaded fully.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
You might also try to press the button only when it is available.
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))
You can try javascript click:
element = driver.find_element_by_xpath('//*[#id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button')
driver.execute_script("arguments[0].click();", element)
In this method, the scrolling shouldn't be necessary. See more info about the subject here.
I'm trying to access a homepage, make login, click the login button and click a button (in the second page) using python/selenium.
The login button I wrote using Xpath and it's working well. The browser opens, writes user and login and click the login button.
Unfortunately, in the next page I'm not able to click on the button that I need to. It didn't work using Xpath and I can't understand why. The html is completely different from the first button, the button name is 'Reservar' and it is inside a class named <app-menu__menu>, written as:
<a href="/Services" id="advanced" class="element ">
<span>Reservar</span>
</a>
The xpath I got and tried:
xpath = "//*[#id="advanced"]"
Then I tried a second verion (it was gotten as the second line code xpath):
xpath = "//*[#id="advanced"]/span"
When I first tried to used them, I got an error. Then I change the "" to ' ' and the error was gone. But the program can't locate the button.
I'm using google-chrome, ubuntu, python3 and selenium package:
driver.find_element_by_xpath(xpath).click()
Thanks for any help.
You probably missing a delay / wait.
After clicking the submit / login button on the login page it takes some time to make the internal page loaded.
The recommended way to do that is to use Expected Conditions explicit waits, something like this:
wait.until(EC.visibility_of_element_located((By.XPATH, "//*[#id='advanced']"))).click()
To use the wait object here you will need to import the following inports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
And to initialize the wait object with
wait = WebDriverWait(driver, 20)
You will have to validate your locator is unique. As well as to validate the element you are trying to access is not inside iframe and not in a new window / tab etc.
Happy Sunday,
Trying to click a filter button (called offer) on a website. It is a hover button that does not have an associated link so cannot find element by link text.
When clicking Offer button, the wanted button goes from class="item-list-header-filter-icon item-list-wanted-filter hover-state" to class="item-list-header-filter-icon item-list-wanted-filter hover-state inactive"
I have tried:
driver.find_element_by_class_name(item-list-header-filter-icon item-list-wanted-filter hover-state)
and since it is a dynamic, I made webdriver wait:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "item-list-header-filter-icon item-list-wanted-filter hover-state inactive"))
)
element.click()
Any ideas much appreciated!
Have a good day
You need to perform a hover over that element.
It can be done as following:
from selenium.webdriver.common.action_chains import ActionChains
offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')
hover = ActionChains(driver).move_to_element(offer)
hover.perform()
In case all you asked for is to locate that element you can simply do it with
offer = driver.find_element_by_css_selector('.item-list-header-filter-icon.item-list-offer-filter')
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)
I'm trying to selenium click on the "next" button at the bottom of the page (just as shown by cursor in image)
Here's the link to full web: http://hr.jsbchina.cn/zp/trs/hotPostList.do
I've tried three methods:
1) The conventional click
nextbutton = browser.find_element_by_xpath('/html/body/form/table[5]/tbody/tr/td[2]/a')
nextbutton.click()
browser.implicitly_wait(10)
2) The "element_to_be_clickable"
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/form/table[5]/tbody/tr/td[2]/a'))).click()
time.sleep(10)
3) I've also tried adding the page number to the page "text field" and clicking on "Go". And I've tried resizing window size as follows:
browser.set_window_size(1920, 1080)
browser.implicitly_wait(10)
Are there any other methods to click on "next"? Because the rest can't seem to work.
Below locators should help you.
driver.find_element_by_link_text('next')
OR
driver.find_element_by_partial_link_text('next')