Selenium is misclicking - python

I'm trying to make Selenium click a button, Selenium has to scroll in order to click that button. But what happens is that when Selenium scrolls down, the button gets behind the sticky bar and Selenium clicks on the sticky bar instead of the button.
I'm using Firefox webdriver.
I tried the following:
DesiredCapabilities.FIREFOX["elementScrollBehavior"] = 1
self.driver = webdriver.Firefox()
When I have elementScrollBehavior set to 1, then it doesn't scroll at all.
How can I make Selenium scroll down properly so that the button doesn't get behind the sticky bar?
-edit-
DesiredCapabilities.FIREFOX["elementScrollBehavior"] = 1
does work now, but it still isn't able to click on the button. Even tho the sticky bar doesn't get in the way now.

You could implement something like this:
driver = webdriver.Firefox()
driver.get("http://somewebpage.example")
time.sleep(5)
while True:
try:
# Edit this to how you're currently scrolling
driver.execute_script("window.scrollBy(0, 150);")
button = driver.find_element(By.XPATH, YOUR_BUTTON_XPATH)
button.click()
break
except WebDriverException:
driver.execute_script("window.scrollBy(0, -100);")
time.sleep(2)
If your script scrolls to far and throws an exception, it will catch this error and scroll back up the page 100 pixels.
This should guarantee your button gets clicked, it will constantly move 50px down the page until it finds it but you can implement your normal way of scrolling and then catch the WebDriverException and scroll back up a couple of pixels.

You could try to use a javascript scroll to element then scroll a little more with an offset
eg in c#.
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("arguments[0].scrollIntoView(true)", element)
jse.ExecuteScript("window.scrollBy(0,50)");

Related

Unable to press and hold on pop menu using selenium in python

I am getting trouble while shifting from the previous window to pop window and then pressing this press and hold button as we cannot able to get the id or class because it's dynamically changing. Is there any solution to get rid of this pop menu by long press and hold using selenium lib?
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[contains(.,'Press and Hold')]//preceding::input[1]")))
# element = driver.find_element_by_xpath("/html/body/div/div/div[2]/div[2]/p")
print("backing ot forward....")
action = ActionChains(driver)
action.click_and_hold(element)
action.perform()
time.sleep(10)
action.release(element)
action.perform()
time.sleep(0.2)
action.release(element)
```
Above are the lines of code that I am trying to run but due to dynamic id, it doesn't work well.
Here is something you may try. I had a similar issue. This worked for me.
The key is the release after 10 seconds (the timing might be different for your use case) and clicks again
element = driver.find_element_by_css_selector('#id-captcha') # captcha id
action = ActionChains(driver)
click = ActionChains(driver)
action.click_and_hold(element)
action.perform()
time.sleep(10) # change it as required
action.release(element)
action.perform()
time.sleep(0.2)
action.release(element)

selenium form submit erases required camps although button is visible and clickable

I am using selenium with chromedriver. I fill a form where there is a required field. Then I click the submit_and_stay button. Normally the click should send the info to the server and create an entry. This has a java-script. The click works but the required field disappears and the submit restarts. Other fields remain visible.
I have explicit wait,
I have scrolled to the button, I have hovered over it and I use EC.until it is clickable.
But still it fails to work. I used normal click, submit, key send return, then I used actions click and still it fails.
Here is the code:
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
time.sleep(10)
id_box=driver.find_element_by_name("submitAddproductAndStay")
id_box=WebDriverWait(driver,
50).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#product-seo >
div:nth-child(8) > button:nth-child(3)')))
ActionChains(driver).move_to_element(id_box).perform()
driver.execute_script("arguments[0].scrollIntoView(true);", id_box);
time.sleep(5)
action = ActionChains(driver)
action.click(on_element = id_box).perform()
Any help?
After loosing 2 days on this, I figured out that the only way to go around this is to execute the click event by direct execute.javascript in the code:
driver.execute_script("arguments[0].click();", element)
As strange as it can be!!

Selenium + Python Click on Search Selects but Does Not Click

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")

Selenium Python - Can't click on element

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')

How can I make Selenium click on the "Next" button until it is no longer possible?

I would like to write a code that would make Python scrape some data on a page, then click on the "next" button at the bottom of the page, scrape some data on the second page, click on the "next" button, etc. until the last page, where clicking on "Next" is no longer possible (because there is no "next").
I would like to make the code as general as possible and not specify beforehand the number of clicks to be done.
Following this question (How can I make Selenium click through a variable number of "next" buttons?), I have the code below. Python does not report any error, but the program stops after the first iteration (after the first click on the "next").
What am I missing here? Many thanks!
driver = webdriver.Firefox()
driver.get("http://www.mywebsite_example.com")
try:
wait = WebDriverWait(driver, 100)
wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'reviews_pagination_link_nav')))
driver.find_element_by_class_name("reviews_pagination_link_nav").click()
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))
while EC.element_to_be_clickable((By.CLASS_NAME,'reviews_pagination_link_nav')):
driver.find_element_by_class_name("reviews_pagination_link_nav").click()
if not driver.find_element_by_class_name("reviews_pagination_link_nav"):
break
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))
finally:
driver.quit()
I would make an endless while True loop and break it once there is TimeoutException thrown - this would mean there are no pages to go left:
wait = WebDriverWait(driver, 10)
while True:
# grab the data
# click next link
try:
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'reviews_pagination_link_nav')))
element.click()
except TimeoutException:
break
For this to work, you need to make sure that once you hit the last page, the element with class="reviews_pagination_link_nav" is not on the page or is not clickable.

Categories

Resources