0

I'm sure that this has probably been asked before, but I have yet to encounter it. My apologies if it is already up here. However, I'm having an issue trying to figure the logic out: The page begins with a list of elements (20 to begin) I'm looking to click. After a scroll, more of the same elements are displayed. I'm having an issue getting to interact with loaded elements after the scroll. This is what I've come up with so far:

def clix():
chazz = driver.find_elements_by_css_selector("button[class^='message-anywhere']")
for x in chazz:
    if x.is_displayed():
            x.click()
            time.sleep(1)
            driver.find_element_by_css_selector("button[data-control-name^='overlay.close']").click()
            time.sleep(2)

scrollz()
def scrollz():
    driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    time.sleep(4)
    clix()
clix()

I know this isn't as 'pythonic' or 'best practices' as it should be. I'm simply worried about functionality. Any insight would be much appreciated. The driver.find_element_by_css_selector("button[data-control-name^='overlay.close']").click() button is only to x out a pop up window.

Thanks

html:

<button class="message-anywhere-button artdeco-button artdeco-button--secondary artdeco-button--2" aria-label="Send message to Abarna Rajkumar" data-ember-action="" data-ember-action-63="63">
    Chat
</button>
2
  • Could you post some HTML for the page you are trying to automate, including the elements you are locating in this test? This will help anyone trying to answer solve your issue more efficiently. Commented Nov 13, 2019 at 16:51
  • I added the html of the button. Identifying the button hasn't been the problem. The script just gets lost in where it is in the indexing once further elements are loaded, and I don't know how to write the logic for that. Commented Nov 13, 2019 at 17:22

2 Answers 2

1

I think Only one function can resolve your problem.I might wrong since I don't have application under test.

  • Induce WebDiverWait And wait for visibility_of_all_elements_located()
  • While iterating use location_once_scrolled_into_view and then click.
  • Induce WebDiverWait And wait for element_to_be_clickable() to click the close button

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def clix():
 chazz =WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"button[class^='message-anywhere']")))
 for x in range(len(chazz)):
    chazz = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "button[class^='message-anywhere']")))
    chazz[x].location_once_scrolled_into_view
    chazz[x].click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[data-control-name^='overlay.close']"))).click()

clix()
Sign up to request clarification or add additional context in comments.

Comments

0

chazz = driver.find_elements_by_css_selector("button[class^='message-anywhere']") global xx xx = 0 def clix(): global xx print (xx) chazz = driver.find_elements_by_css_selector("button[class^='message-anywhere']") while xx < len(chazz): print(xx)

    chazz[xx].click()
    time.sleep(1)
    driver.find_element_by_css_selector("button[data-control-name^='overlay.close']").click()
    time.sleep(1)

    xx = xx+1

driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
xx = xx+1

i = 0

while i < 1000:
    clix()

and if I truly wanted to run it into infinity... I'd just set the while loop to something unachievable.... while i < -1 ... fox example.

Comments

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.