Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Counting Instances of an element with selenium and python

$
0
0

so I am working on a online class that requires me to use selenium to navigate to * Navigate to The Internet website (https://the-internet.herokuapp.com) - then to a specific child page (add/remove elements)then add some elements and count them and remove some and count the result. my first approach was rather ramshackle. I was able to get it to "work" (i.e correctly navigate and click elements) but unable to fill all the requirements (i.e I could not keep an a variable count and I didn't know how to add assertions)

So I settled on this approach

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ecclass Webpage:    element_count = 0    def __init__(self, element_count, driver):        self.element_count = element_count        self.driver = driver    driver = webdriver.Firefox()    try:        wait = WebDriverWait(driver, 10)        driver.get('https://the-internet.herokuapp.com')# Parent page loads        wait.until(ec.url_to_be('https://the-internet.herokuapp.com/'))# Variables go here (as they depend on parent loading)        add_remove_parent = wait.until(ec.presence_of_element_located(        (By.LINK_TEXT, 'Add/Remove Elements')))        driver.fullscreen_window()    # This is where the functions happen        add_remove_parent.click()        #  * Assert that no elements have yet been added        wait.until(ec.url_to_be('https://the-internet.herokuapp.com/add_remove_elements/'))        # Child Page Loaded        wait.until(ec.element_to_be_clickable((By.XPATH, '//button[text()="Add Element"]')))        def add_element(self):            add_element_button = self.driver.find_element(By.XPATH, '//button[text()="Add Element"]')            add_element_button.click()            self.element_count += 1            print(f'The element count is {self.element_count}')        def delete_element(self):            delete_element_button = self.driver.find_element(By.CLASS_NAME, 'added-manually')            delete_element_button.click()            self.element_count -= 1            print(f'the element count is {self.element_count}')    webpage=Webpage(self,element_count=0 ,driver)    finally:        # Close the browser        driver.quit()#

But , (and here is where I am getting it wrong) I don't seem to be (Instantiating?) if that is the right word -> the class correctly and would like some direction with this.As well as maybe some direction on correctly calling the methods on it. I've done it with simpler things , and i've done it with alot of the code abstracted away. But never on my own from scratch and I can't yet see what I'm missing.

Also, I know I don't have any assertions yet , I'll try to add those in after , maybe into the methods themselves or maybe separate.

Most times when I've tried the class does not seem to understand what the argument for self , or driver should be but I thought I provided that by saying it should be firefox.

Much appreciated for any help!


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>