1

I'm experimenting witch selenium in Python. I'm trying to click at up or down vote button below the comment. I'm using XPath to determinate specyfic button. There's no error occured but counter doesn't increase after clicking. I have tried on different webpages but results are same.

My first approach was that, I have used find_element_by() function but after that I could't use click() method on returned element. Now I'm useing ActionChains This is my script

from selenium import webdriver
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

import time

driver = webdriver.Firefox()

driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
driver.maximize_window()
wait = WebDriverWait(driver,30)
action = ActionChains(driver)

cookieButton = wait.until(EC.element_to_be_clickable((By.ID,"inforcwp-y")))
cookieButton.click()

time.sleep(5)

#wait.until(EC.visibility_of((By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[9]/div[2]/div/ul/li[20]/p[1]/span[4]/a[2]")))

element = driver.find_element(By.XPATH,"/html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/ul/li[8]/p[1]/span[4]/a[2]")


element.location_once_scrolled_into_view

time.sleep(5)
action.double_click(element)

time.sleep(5)
driver.quit()

I'm expecting to increase up/down vote cunter after clicking on "voting hand" Please give mo some advices how to achive my goal

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
pythonBeginer
  • 41
  • 1
  • 8
  • Not sure why you are using automation for the `upvote/downvote`.... Anyhow, have you tried with `element.click()`? what was the error? – supputuri Jul 22 '19 at 14:16
  • 1
    It seems like the webpage is buggy itself. When I manually click the upvote, sometimes the counter goes up, sometimes the page scrolls to the top, and sometimes the counter goes up by more than one. Can you check if it works on other webpages? Also apparently [the behaviour of `location_once_scrolled_into_view` is unreliable.](https://github.com/SeleniumHQ/selenium/issues/4280) so you might want to look into that. – emilanov Jul 22 '19 at 14:20
  • @supputuri 1 it's for educational purpouses 2 has no method click() @ emilianov I have check on 3 different pages. I have noticed same facts as You and one more. When I'm not using browser by selenium(remote mod) everythink works fine. – pythonBeginer Jul 22 '19 at 14:39

1 Answers1

1

To click() on the upvote icon you need to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    chrome_options = webdriver.ChromeOptions() 
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument('disable-infobars')
    driver = webdriver.Chrome(options=chrome_options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://forsal.pl/praca/wynagrodzenia/artykuly/1422953,nik-w-nbp-sa-nieprawidlowosci.html")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID,"inforcwp-y"))).click()
    driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH,"//span[@class='headerUnderline' and contains(., 'Komentarze')]"))))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//ul[@id='commentsList']/li/p//span[@class='kf-rating']//a[@class='ratingUp']"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank You for reply Is scrollong script necessary? – pythonBeginer Jul 22 '19 at 15:01
  • @pythonBeginer Good question, seems to me scrolling is necessary as the desired elements are initially not interactable unless you scroll them within the _ViewPort_ – undetected Selenium Jul 22 '19 at 15:07
  • Sir could You explain where You found "//ul[@id='commentsList']/li/p//span[@class='kf-rating']//a[@class='ratingUp']" XPATH ? When I'm clicking copy XPath I'm receiving this /html/body/div[2]/section/div[2]/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/ul/li[12]/p[1]/span[4]/a[1] – pythonBeginer Jul 22 '19 at 15:25
  • @pythonBeginer You have used _absolute xpath_ and that's the way how _automation engineers_ would get started to write _xpaths_. Pat yourself, you are on the right track. Gradually you should start writing _loical xpath_ on your own following this [discussion](https://stackoverflow.com/questions/46700764/how-to-inspect-element-in-selenium3-6-as-firebug-is-not-an-option-any-more-for-f/46702281#46702281) – undetected Selenium Jul 22 '19 at 19:54