Answer the question
In order to leave comments, you need to log in
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("Ничего не обнаружено")
Answer the question
In order to leave comments, you need to log in
Like this
unread = [];
unread=driver.find_element_by_class_name('under');
completed = [];
completed=driver.find_element_by_class_name('completed');
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("Ничего не обнаружено")
#Что происходит?
#вот тут у вас переменная логично названа: 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')
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 questionAsk a Question
731 491 924 answers to any question