Reputation: 35
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()
Upvotes: 0
Views: 699
Reputation: 1821
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()
Upvotes: 0
Reputation: 193088
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
Upvotes: 2