Forums

Selenium locating wrong elements

i want to locate count of elements which depends on the user account if the user has some stocks in the account then the element will appear other than that it will not so i have used css selector to locate the element because all the stock name has same css selector so it will give the list of elements and by list i can have the text of the elements and for that the code is given below.

### code
    form = WebDriverWait(self.driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.app')))
    time.sleep(1)
    print('oo')
    z = self.driver.find_element(By.CSS_SELECTOR, 'a[href="/holdings"]')

    z.click()
    print('kk')
    form = WebDriverWait(self.driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]")))
    # time.sleep(1)
    # form = WebDriverWait(self.driver, 10).until(
    #    EC.visibility_of_element_located((By.CSS_SELECTOR, "td[class='instrument right-border']")))
    time.sleep(1)
    count_of_stocks = self.driver.find_elements(By.CSS_SELECTOR, "td[class='instrument right-border']")
    count = 0
    print("no. of stocks")
    print(len(count_of_stocks))
    for i in count_of_stocks:

        print(i.text)
### end

so this gives the output below:

### output

oo kk no. of stocks 10 APTUS DABUR DEVYANI FEDERALBNK GLS ITC JUBLFOOD MON100 TATACONSUM TATAMTRDVR

but when i try to run this same code in python_anyhwere it give the count wrong and not been able to locate the correct elements

and the output it give does not matches the output generate by the same code in local machine. Output generated by the python anywhere is given beloe:

####### output

oo kk no. of stocks 0

Questions : 1. Why does the output mismatch in python anywhere and local machine. 2. Why does the some elements work good in local machine but when the same element we run in python anywhere show the error that it is not able to locate the element.

  1. Check the HTML that your're getting back so you can see where the differences are
  2. There are many possibilities. The most likely is that the service provides different pages to different geographical locations.
  1. how to check the actually what do you meant by html

and i still cant understand why pyhton anywhere is showing error because the element can only be located through xpath because every element is dynamic and generated in same way vertically example of it is the stock holding in a demat account so every detail is sequentially arranged depending on the different stocks you hold , as the arrangement is same the class and css locator for the stocks will be same, so only different thing which can help in locating distinct element can be xpath and for xpath it shows the error of element not found.

If the content is added dynamically after the page load, you probably want to get a screenshot of the page so you can see what is actually on the page: https://selenium-python.readthedocs.io/faq.html?highlight=screenshot#how-to-take-screenshot-of-the-current-window