1

I want to scroll the contacts of web WhatsApp and click on the particular contact. I am able to scroll the contacts but not able to find the specific element to get particular contact because HTML is dynamic, as I scroll the contacts it gets changed. Here is the code:

target = '"name"'
panel = driver.find_element_by_class_name('_1NrpZ')
a = 0
while elem is None:
  a += 5
  try:
      driver.execute_script('arguments[0].scrollTop = %s' %a, panel)
      elem = driver.find_element_by_xpath('//span[@title=' + target + ']')
      time.sleep(5)
  except:
       pass

ac = ActionChains(driver)
ac.move_to_element(elem).click().perform()
time.sleep(2)

It does not stop and keeps scrolling until the last contact. I think when I scroll new HTML is not loaded so is there any solution?

1 Answer 1

1

I've found that time.sleep(x) is usually the problem when writing similar code for Selenium. The problem is that it pauses all computation and tends to cause more harm than good, at least in my experience. I try to use Expected Conditions whenever possible for this type of thing.

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

wait = WebDriverWait(my_selenium_driver, 15)    
        my_button = wait.until(EC.element_to_be_clickable((By.XPATH,self._my_button_xpath))).click()

As an example, you can keep your current action chain that is in charge of scrolling through the list and allow it to do so but during that scroll have the EC.wait looking for your desired element and clicking it as soon as it is visible.

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

5 Comments

Thanks for your reply but actually, that element itself is not clickable. I will need to click nearby that element if we find that.
when I use elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.xpath, '//span[@title="Prakash Sharma"'))) then also it keeps scrolling. this doen't even wait for the element.
Is this a public website that I could see the element? With EC you can move to an element and just send a click rather than explicitly clicking that element. It's a bit difficult for me to help write the needed selenium code without being able to see the HTML, however.
yes, It is a public website but you will need to also install WhatsApp app on your mobile and log in. It's a social media messaging website.
Oh gotcha. I can maybe help further if it's still not working. Are you getting any error or is it just not locating the element? I made a mistake in my original answer and left EC.element_to_be_clickable but I think you might have better luck using thing = wait.until(EC.visibility_of_element_located(By.XPATH, "XPATH")) as the docs on that function state it returns true when the element is both rendered and has height/width > 0

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.