【问题标题】:locating textbox in facebook messenger在 facebook messenger 中定位文本框
【发布时间】:2021-08-26 04:44:42
【问题描述】:

我是一个菜鸟 python 编码器。我开始学习 selenium 模块,现在我想使用它发送文本,但我很难找到文本框,因为每次刷新浏览器时文本框的 xpath 都会发生变化

from selenium import webdriver
import time

i=1
text=input("enter the text messege")
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
#driver=webdriver.Chrome(r"chromedriver")
driver.get(r"https://www.facebook.com/messages/t/2585929161530825/")
time.sleep(7)
email_box=driver.find_element_by_xpath(r'//*[@id="email"]')
email_box.click()
email_box.send_keys("xnxx@gmail.com")
time.sleep(3)
password_box=driver.find_element_by_xpath(r'//*[@id="pass"]')
password_box.click()
password_box.send_keys("rockyou.txt")
time.sleep(3)
login_button=driver.find_element_by_xpath(r'//*[@id="loginbutton"]')
login_button.click()
time.sleep(10)
'''remove_exception=driver.find_element_by_xpath(r'//*[@id="mount_0_0_DP"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[1]/div[1]/div/div/div[3]/div/div[15]/div[2]/div[3]/div/div/div/div[1]/div/div[1]/span/div/div')
remove_exception.click()
time.sleep(4)'''
#pop_up=driver.find_element_by_xpath(r'')
while i!=3000:
     chat_box=driver.find_element_by_class_name(r'ecm0bbzt e5nlhep0 buofh1pr jq4qci2q a3bd9o3v iko8p5ub eg9m0zos ni8dbmo4 rq0escxv lexh0w6q')
     chat_box.click()
     chat_box.send_keys(text,"{}".format(i))
     send_button=driver.find_element_by_xpath(r'//*[@id="mount_0_0_Td"]/div/div[1]/div/div[3]/div/div/div[1]/div[1]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/form/div/div[3]/span[2]/div/svg')
     send_button.click()
     if(i==5):
          time.sleep(2)
     i+=1

【问题讨论】:

    标签: python selenium xpath automation


    【解决方案1】:

    您使用了错误的定位器。
    好吧,Facebook 元素定位器不好 :)
    请试试这个:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    
    chat_box_css = 'div[aria-describedby]'
    send_btn_css = 'div[aria-label="Press Enter to send"]'
    while i!=3000:
         chat_box=driver.find_element_by_css_selector(chat_box_css)
         chat_box.click()
         chat_box.send_keys(text,"{}".format(i))
         wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, send_btn_css))).click()
         
    
    

    【讨论】:

    • 谢谢你先生会试试这个,还有一个帮助先生,你如何找到聊天框的css,我的意思是你从哪里复制了那个css,谢谢你,你教我有功能类似于等待元素可见性定位
    • 我没有复制选择器。您应该学习如何创建正确的选择器。自动提供的定位器几乎都是无用的
    • 先生,我编辑了您提到的代码,但是我面临 sme 错误 ``` selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:指定了无效或非法选择器(会话信息:chrome=91.0.4472.77)你能否说明原因和解决方案,@Prophet
    • 我已经提到了上面的错误...指定了一个无效或非法的选择器,在它没有显示这些错误之前,我认为那个 css 选择器有问题
    • 但它应该显示不正确的定位器表达式。
    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多