0

Can you tell me why at this website: https://pasja-informatyki.pl/programowanie-webowe/test/przeglad-html/ I have no idea why it's not clicking the button. Maybe the alert after clicking is messing with selenium? I tried differend selectors and didn't work. I hope someone can help me :D

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import os

class Bot:
    def __init__(self):
        self.driver = webdriver.Firefox()
        self.action = ActionChains(self.driver)
        self.driver.maximize_window()
        self.driver.get("https://pasja-informatyki.pl/programowanie-webowe/test/przeglad-html/")
        sleep(1)
        x = 1
        self.driver.find_element_by_id("hcks").click()
        for x in range (10):
            #pytanie = self.driver.find_element_by_partial_link_text(f"{x+1}.").text
            pytanie = self.driver.find_element_by_xpath(f"//div[contains(text(),'{x+1}.')]").text
            f = open("pio.txt", "a")
            f.write(pytanie + "\n")
            f.close()

            self.driver.find_element_by_id(f"odpa{x+1}").click();
            self.driver.execute_script("window.scrollTo(0, 500)")

        self.driver.execute_script("window.scrollTo(0, 3500)")
        sleep(1)
        self.driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))))
        self.driver.find_element_by_xpath("//*[@id='przycisk']").click()

        #self.action.move_to_element(przycisk).click(sprawdz).perform()
        self.driver.find_element_by_xpath("//button[@id='sprawdz']").click();
        #self.driver.find_element_by_link_text("ok").click();
        for x in range (10):
            odpowiedz = self.driver.find_element_by_xpath(f"//div[contains(text(),'odp{x+1}') and @class='odpgood']").text
            f = open("pio.txt", "a")
            f.write(odpowiedz + "\n")
            f.close()


Bot()
2
  • You do have a semicolon oddly in several areas. Commented Dec 28, 2020 at 12:52
  • thanks, i deleted them, but it seems they didn't cause any problem Commented Dec 28, 2020 at 12:59

2 Answers 2

2

To click on Rozumiem you can use either of the following Locator Strategies:

  • Using id:

    driver.find_element_by_id("hcks").click()
    
  • Using css_selector:

    driver.find_element_by_css_selector("span#hcks").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//span[@id='hcks']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using ID:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "hcks"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#hcks"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='hcks']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Sign up to request clarification or add additional context in comments.

4 Comments

it's not clicking this damn button xD wtih id @id='sprawdz'. So annoying xD
can you run this code and see why it's not working?
@MikołajK using WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "hcks"))).click() will uliminate the need of the sleep's
ye but the previous button with id sprawdz creates problem
0

Your actions.perform() section is commented. Ensure you call the perform() method to Performs all stored actions.

#self.action.move_to_element(przycisk).click(sprawdz).perform()

If this does not work can you try without ActionChains?

Following worked and button was closed.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://pasja-informatyki.pl/programowanie-webowe/test/przeglad-html/")
assert "Test wiedzy - 10 pytań" in driver.title
elem = driver.find_element_by_id("hcks")
elem.click()

3 Comments

it's commented because it didn't work :/ I have no clue why this specific button can't be clicked
Sample code can be found github.com/pragmatictesters/…. Can you give a try without ActionChains?
BTW I started with Python + Selenium. You code worked successfully. I ran the code on Mac 10.15.7 (19H15) Catalina, Python 3.7.6 screencast.com/t/riqZTyzzsA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.