I have done the next function to use twitter with Selenium.
If i put the HeadLess = False, it works perfect but when I put HeadLess = True, it doesnt find the elements by the class.
Some Help?
opciones=Options()
opciones.add_experimental_option('excludeSwitches', ['enable-automation'])
opciones.add_experimental_option('useAutomationExtension', False)
opciones.headless=False
def twittear():
url = 'https://twitter.com/login'
driver=webdriver.Chrome(PATH, options=opciones)
driver.get(url)
try:
# cookies
driver.find_element_by_class_name('css-901oao css-16my406 css-bfa6kz r-poiln3 r-a023e6 r-rjixqe r-bcqeeo r-qvutc0'.replace(' ', '.')).click()
time.sleep(1)
except:
None
# Introduzco el mail
time.sleep(10)
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(user)
time.sleep(2)
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(u'\ue007')
time.sleep(3)
# Introduzco username
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(user)
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(u'\ue007')
time.sleep(3)
# Introduzco password
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(password)
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(u'\ue007')
time.sleep(3)
# Twitt
driver.find_element_by_class_name('public-DraftStyleDefault-block public-DraftStyleDefault-ltr'.replace(' ', '.')).send_keys(gift_link)
driver.find_element_by_class_name('css-18t94o4 css-1dbjc4n r-l5o3uw r-42olwf r-sdzlij r-1phboty r-rs99b7 r-19u6a5r r-2yi16 r-1qi8awa r-1ny4l3l r-ymttw5 r-o7ynqc r-6416eg r-lrvibr'.replace(' ', '.')).click()
CodePudding user response:
To be honest, I'm surprised it finds the elements at all, headless or not, as Selenium doesn't allow to locate elements by compound classes.
Try doing this, see if it makes a difference:
Change this line:
driver.find_element_by_class_name('r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu'.replace(' ', '.')).send_keys(user)
to:
driver.find_element_by_xpath('//*[contains(@class, "r-30o5oe") and contains (@class, "r-1niwhzg")]').replace(' ', '.')).send_keys(user)
CodePudding user response:
I have tried with that Xpath but it doesnt work. It works if i use the Xpath like in the following code. But it happens the same. With HeadLess = False, it works, with Headless = True, doesnt works.
def twittear():
url = 'https://twitter.com/login'
driver=webdriver.Chrome(PATH, options=opciones)
driver.get(url)
try:
# cookies
driver.find_element_by_class_name('css-901oao css-16my406 css-bfa6kz r-poiln3 r-a023e6 r-rjixqe r-bcqeeo r-qvutc0'.replace(' ', '.')).click()
time.sleep(1)
except:
None
# Introduzco el mail
time.sleep(10)
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input').send_keys(user)
time.sleep(2)
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[5]/label/div/div[2]/div/input').send_keys(u'\ue007')
time.sleep(3)
# Introduzco username
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[3]/div/label/div/div[2]/div[1]/input').send_keys(user)
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[3]/div/label/div/div[2]/div[1]/input').send_keys(u'\ue007')
time.sleep(3)
# Introduzco password
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[3]/div/label/div/div[2]/div[1]/input').send_keys(password)
driver.find_element_by_xpath('//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div[3]/div/label/div/div[2]/div[1]/input').send_keys(u'\ue007')
time.sleep(3)
# Twitt
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/label/div[1]/div/div/div/div/div[2]/div/div/div/div').send_keys(gift_link)
driver.find_element_by_xpath('//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[3]/div/div/div[2]/div[3]/div/span/span').click()