Как отправить список слов, одно за другим в чате на Facebook, используя Selenium и Python - PullRequest
1 голос
/ 25 апреля 2020
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

stringOfWords = "this is a sentance I want to send word by word"
listOfWords = stringOfWords.split(" ")

driver = webdriver.Edge('C:/Users/Andrej/OneDrive - UKIM, FINKI/Kodovi/Python/msedgedriver.exe')
user = '*myusername*'
pw = '*mypassword*' 

driver.get("https://facebook.com")
sleep(2)
driver.find_element_by_xpath("//input[@name=\"email\"]").send_keys(user)
driver.find_element_by_xpath("//input[@name=\"pass\"]").send_keys(pw)
sleep(1)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input').click()
sleep(6)
driver.find_element_by_xpath('/html/body/div[1]/div[2]/div/div[1]/div/div/div/div[2]/div[2]/div[2]/div/a').click()
sleep(1)
driver.find_element_by_xpath("//span[.='*the name of the person I want to send it to*']").click()

#everything works fine until here
msgbox = driver.find_element_by_xpath('/html/body/div[1]/div[7]/div[1]/div/div/div[4]/div/div[1]/div/div/div/div/div/div[1]/div/div[2]/div[4]/div/div/div/span/div/div/div[2]/div/div/div/div')

for word in listOfWords:
    if word != ' ':
        msgbox.send_keys(word)
        sleep(2)
        msgbox.send_keys(Keys.RETURN)
        sleep(2)

Работает нормально до второго слова. Он отправляет первый, но после второго он вылетает и выдает мне «StaleElementReferenceException».

Я знаю, что делаю что-то совершенно очевидно, но я нигде не могу найти решение

(Я использую Edge Chromium в качестве браузера, но я получаю те же результаты на Chrome)

Ответы [ 2 ]

1 голос
/ 25 апреля 2020

Мне кажется, это работает, проверьте комментарии ниже:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys


driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://facebook.com')
wait = WebDriverWait(driver, 10)

el = wait.until(EC.visibility_of_element_located((By.ID, "email")))
el.send_keys("username")
el = wait.until(EC.visibility_of_element_located((By.ID, "pass")))
el.send_keys("password")
el.send_keys(Keys.ENTER)

driver.refresh() # intentionally here, otherwise, in my case, was throwing an err error: "the element is no longer attached to the DOM"...

el = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Chat']")))
el.click()

el = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(text(), 'Kelly')]"))) # change to the full or partial friend name
el.click()

el = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'notranslate')]")))
el.click()

s = "this is a sentEnce I want to send word by word"
for w in s.split():
    el.send_keys(w)
    el.send_keys(Keys.ENTER)

enter image description here

1 голос
/ 25 апреля 2020
Try below solution ::


from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_argument('disable-infobars');
options.add_argument("disable-notifications");
driver = webdriver.Chrome(executable_path="C:\New folder\chromedriver.exe",chrome_options=options)
stringOfWords = "this is a sentance I want to send word by word"
listOfWords = stringOfWords.split(" ")


wait = WebDriverWait(driver, 20)
driver.get('https://facebook.com')
driver.maximize_window()

driver.find_element_by_xpath("//input[@name=\"email\"]").send_keys("credential")
driver.find_element_by_xpath("//input[@name=\"pass\"]").send_keys("credential")

wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Log In']"))).click()
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@name='mercurymessages']//div[@class='_2n_9']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Friend name')]"))).click()
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@role, 'presentation')]//div[contains(@aria-describedby, 'placeholder')]")))
actionChains = ActionChains(driver)
actionChains.move_to_element(element).click().perform()
text = "this is a sentance I want to send word by word"
for world in text.split():
    element.send_keys(world)
    element.send_keys(Keys.ENTER)

Выход:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...