this is the code: def findandclickbuttoncsgoroll2(driver, team1, team2, buttonstring):…
- User
this is the code: def find_and_click_button_csgoroll_2(driver, team_1, team_2, button_string): forbidden_strings = [":", ""] found_team_1 = False found_team_2 = False counter = 0 elements = driver.find_elements(By.XPATH, "//*") previous_element = None for element in elements: print(element.text) print("-" * 20) if team_1 in element.text and not found_team_1: print("found the first team!") found_team_1 = True if found_team_1: print("counter!", counter) counter += 1 if found_team_1 and not found_team_2 and counter > 100: print("False!") found_team_1 = False counter = 0 if found_team_1 and found_team_2 and button_string in element.get_attribute("outerHTML"): print("clicked on the odd!") while previous_element is not None: try: button = previous_element.find_element(By.XPATH, ".//button") # Scroll to the button element driver.execute_script("arguments[0].scrollIntoView();", button) actions = ActionChains(driver) actions.move_to_element(button).click().perform() return True except: parent_element = previous_element.find_element(By.XPATH, "..") if parent_element is None: break previous_element = parent_element previous_element = element return False instead of going to the previous element, when the element found go to element parent and that go one element previous that supposed to be button type, every time element is button type with event called click set previous element to the current one
- Assistant
This is the modified code:
def find_and_click_button_csgoroll_2(driver, team_1, team_2, button_string): forbidden_strings = [":", ""] found_team_1 = False found_team_2 = False counter = 0 elements = driver.find_elements(By.XPATH, "//*") previous_element = None for element in elements: print(element.text) print("-" * 20) if team_1 in element.text and not found_team_1: print("found the first team!") found_team_1 = True if found_team_1: print("counter!", counter) counter += 1 if found_team_1 and not found_team_2 and counter > 100: print("False!") found_team_1 = False counter = 0 if found_team_1 and found_team_2 and button_string in element.get_attribute("outerHTML"): print("clicked on the odd!") while previous_element is not None: try: button = previous_element.find_element(By.XPATH, ".//button") if button.get_attribute('type') == 'button' and button.get_attribute('onclick') == 'click': # Scroll to the button element driver.execute_script("arguments[0].scrollIntoView();", button) actions = ActionChains(driver) actions.move_to_element(button).click().perform() return True except: parent_element = previous_element.find_element(By.XPATH, "..") if parent_element is None: break previous_element = parent_element previous_element = element return False