D
D
Dmitry2016-12-26 12:24:31
Python
Dmitry, 2016-12-26 12:24:31

How to implement element search by class, Python3+Selenium?

The task is this, with the help of selenium, I go to the site, log in, and then I need to go over the page and find certain classes, now I’m trying to make them into a list, in the future, of course, I want to go to the dictionary, but for now I’m stuck on this cycle,

unread=driver.find_element(by=By.CLASS_NAME, value='under')  #driver.find_element_by_class_name('under')
completed=driver.find_element(by=By.CLASS_NAME, value='completed')#driver.find_element_by_class_name('completed')
com=[]
for i in driver:
    if i == completed:
        com.append(i)
        print(com)
    elif i==unread:
        com.append(i)
        print(com)
    elif i!=unread and i!=completed:
        print("Ничего не обнаружено")

maybe I'm doing something wrong, but an error occurs:
Traceback (most recent call last):
File "C:/.../PycharmProjects/untitled/directum.py", line 28, in
unread=driver.find_element(by=By.CLASS_NAME , value='under') #driver.find_element_by_class_name("under")
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value })['value']
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35 \lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192,in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"under"}
(Session info: chrome=55.0.2883.87)
(Driver info : chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 10.0.14393 x86_64)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2016-12-26
@ipatov_dn

Like this

unread = [];
unread=driver.find_element_by_class_name('under');
completed = [];
completed=driver.find_element_by_class_name('completed');

Here's what we've come up with so far:
- There are no .unders elements on the page. So in the code you need to write a condition that if (len(unders) > 0), then move on
- there is no clarity with the code further:
unread = [] #вот тут создали массив
unread=driver.find_element_by_class_name('under') #вот тут его ничем не наполнили, т.к. 0

com=[]
for i in driver: #вот это вообще загадочная строка для меня. У вас драйвер - это не коллекция. Это объект другой. может быть, вы хотели for i in unread ?
  if i == completed: #вот тут тоже загадка. вы сравниваете объект типа WebElement с массивом. 
    com.append(i)
    print(com)
  elif i==unread: #и тут
    com.append(i)
    print(com)
  elif i!=unread and i!=completed: #и тут
    print("Ничего не обнаружено")

#Что происходит?

UPD2: and here's another question:
#вот тут у вас переменная логично названа: completed - тут все элементы класса completed
completed=driver.find_element(by=By.CLASS_NAME, value='completed')
#а вот тут переменная unread, а класс under. Так и надо?
unread=driver.find_element(by=By.CLASS_NAME, value='under')  #driver.find_element_by_class_name('under')

UPD3: And in general, read constantly

D
Dmitry, 2016-12-26
@ipatov_dn

Dmitry Eremin

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome(executable_path="C:\Python35\chromedriver.exe")
#driver = webdriver.Firefox(executable_path = "C:\geckodriver.exe")

driver.get("https://directum.adm.yar.ru/Login.aspx") # Запускает браузер
#time.sleep(10) Ждет 10 сек

# Ввод логина
login = driver.find_element_by_id("sLogin")
login.clear()
login.send_keys("login")
# Ввод пароля
pswd = driver.find_element_by_id("sPwd")
pswd.send_keys("pass")
# Жмем войти
driver.find_element_by_id("btnLogin").click()

time.sleep(2) # Ждет 10 сек

button=driver.find_element_by_tag_name('button')
button.send_keys(Keys.ENTER)
time.sleep(5)

driver_new = driver.current_url
print('Новая страница: ', driver_new)

unread = []
unread=driver_new.find_element_by_class_name('under')
completed = []
completed=driver_new.find_element_by_class_name('completed')
com=[]
for i in driver_new:
    if i == completed:
        com.append(i)
        print(com)
    elif i==unread:
        com.append(i)
        print(com)
    elif i!=unread and i!=completed:
        print("Ничего не обнаружено")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question