Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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 specific button. There's no error occured but the counter doesn't increase after clicking. I have tried on different webpages but the results are the same.

My first approach was that I have used find_element_by() function but after that, I couldn't use click() method on returned element. Now I'm using 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 counter after clicking on "voting hand" Please give me some advices on how to achieve my goal.

1 Answer

0 votes
by (62.9k points)

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...