Reddit.com не могу найти элементы Selenium Python "text (optional)" и галочку поставить на "Share this post on Twitter"
Не могу найти элементы Selenium Python "text (optional)" и галочку поставить на "Share this post on Twitter". На примере добавление новости тут Reddit добавление новости
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re,traceback
word_filtr_brakujem_stranicu = ['та страница недоступна',
'сожалению, этот контент сейчас недоступен',
'site can’t be reached',
]
wait_1 = 2
login_passwords = [['name', 'name2','name3'],
['password', 'password2','password3']]
s = 0
class AppDynamicsJob(unittest.TestCase):
def setUp(self):
# AppDynamics will automatically override this web driver
# as documented in https://docs.appdynamics.com/display/PRO44/Write+Your+First+Script
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_app_dynamics_job(self):
driver = self.driver
'''
print('Ожидание...')
time.sleep(wait_1)
driver.maximize_window()
print('Ожидание...')
time.sleep(wait_1)
'''
#_+_+_+
#print('login now: /из...' + str(s) + '/' + str(len(login_passwords[1])))
#print(str(login_passwords[0][s]))
#print(str(login_passwords[1][s]))
#gробуем скрывать окно selenium.minimizeWindow()#"webdriver.Chrome(--headless)"##
driver.get('https://www.reddit.com/login/?dest=https%3A%2F%2Fwww.reddit.com%2F')
print('Ожидание...')
time.sleep(wait_1)
elem = driver.page_source
elem = str(driver.find_element_by_xpath(".//html"))
# страница содержит информацию или она битая
i = 0
while word_filtr_brakujem_stranicu:
if i >= len(word_filtr_brakujem_stranicu):
s = 0
break
if word_filtr_brakujem_stranicu[i] in elem:
i += 1
print('Какие то проблемы со страницой, ждем и пробуем еще раз...')
time.sleep(wait_1 * 5)
else:
i += 1
print('Ожидание...')
time.sleep(wait_1 * 5)
elem = driver.find_element_by_id('loginUsername')
elem.send_keys(login_passwords[0][s]) # вводим значение
elem = driver.find_element_by_id('loginPassword')
elem.send_keys(login_passwords[1][s], Keys.ENTER)
print('Ожидание...')
time.sleep(wait_1 * 5)
#_+_+_+
#_+_++_+
driver.get("https://www.reddit.com/r/telescopes/submit")
print('Ожидание...')
time.sleep(wait_1 * 5)
elem = driver.page_source
elem = str(driver.find_element_by_xpath(".//html"))
try:
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div/div/textarea").click()
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div[2]/div/div/div[3]/div/div/div/div/div").click()
print('Ожидание...')
time.sleep(wait_1 * 3)
except Exception as exc:
print(exc)
traceback.print_exc()
try:
#заголовок
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div/div/textarea").click()
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div/div/textarea").clear()
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div/div/textarea").send_keys(u"аолвыаыв")
print('Ожидание...')
time.sleep(wait_1 * 3)
except Exception as exc:
print(exc)
traceback.print_exc()
try:
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div[2]/div/div/div[3]/div/div/div/div/div").click()
print('Ожидание...')
time.sleep(wait_1 * 3)
except Exception as exc:
print(exc)
traceback.print_exc()
try:
# ERROR: Caught exception [unknown command [editContent]]
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[4]/div/div/div[2]/div/div/div").click()
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[3]/div[2]/div/div/button").click()
print('Ожидание...')
time.sleep(wait_1 * 3)
except Exception as exc:
print(exc)
traceback.print_exc()
try:
#описание и текст
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div[2]/div/div/div[3]/div/div/div/div/div").click().send_keys(u"описание1")
print('Ожидание...')
time.sleep(wait_1 * 5)
except Exception as exc:
print(exc)
traceback.print_exc()
try:
driver.find_element_by_xpath("//div[@id='SHORTCUT_FOCUSABLE_DIV']/div[2]/div/div/div/div[2]/div[3]/div/div[2]/div[3]/div[2]/div[2]/div/div/div[3]/div/div/div/div/div/div/div/div[2]/div").click().send_keys(u"аолвыаыв")
print('Ожидание...')
time.sleep(wait_1 * 5)
except Exception as exc:
print(exc)
traceback.print_exc()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
# To know more about the difference between verify and assert,
# visit https://www.seleniumhq.org/docs/06_test_design_considerations.jsp#validating-results
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
