当我执行Facebook共享按钮时,我遇到了麻烦,但是它没有单击Facebook共享按钮,请帮帮我!
这是我的视频,您可以在这里查看>> https://photos.app.goo.gl/Pi1tRQTwhqg5tmpn9
这是元素HTML代码:
这是元素代码:
<span class="_18vi"><a class=" _2nj7 _18vj _18vk" data-ft="{ "tn": "J", "type": 25 }" href="/ajax/sharer/?s=22&appid=25554907596&id=1816791348467451&p[0]=130584170421519&p[1]=1816792805133972&sharer_type=all_modes&av=130584170421519" rel="dialog" role="button" tabindex="0" title="Send this to friends or post it on your timeline.">Share</a></span>
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://www.facebook.com/")
driver.maximize_window()
phone=WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='email']"))).send_keys("03460522942")
time.sleep(2)
pas=WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='pass']"))).send_keys("......")
time.sleep(.5)
login=WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='loginbutton']"))).click()
time.sleep(.5)
#Searc
search=WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/form[1]/div[1]/div[1]/div[1]/div[1]/input[2]"))).send_keys("lunch.pk")
search2=WebDriverWait( driver, 100).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/form[1]/button[1]"))).click()
click_page=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[3]/div[2]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/a[1]/span[1]"))).click()
click_photos=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[3]"))).click()
click_banner=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "/html[1]/body[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[2]/div[1]/div[1]/a[1]/img[1]"))).click()
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_1b_2']/div[2]/div/div[2]/div/span[3]/a/text()"))).click()
这是我的错误:
Traceback (most recent call last):
File ".\facebook.py", line 24, in <module>
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_1b_2']/div[2]/div/div[2]/div/span[3]/a/text()"))).click()
File "C:\Users\Hamza Lachi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "C:\Users\Hamza Lachi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 297, in __call__
element = visibility_of_element_located(self.locator)(driver)
File "C:\Users\Hamza Lachi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 128, in __call__
return _element_if_visible(_find_element(driver, self.locator))
File "C:\Users\Hamza Lachi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 148, in _element_if_visible
AttributeError: 'NoneType' object has no attribute 'is_displayed'
我
return element if element.is_displayed() == visibility else False
答案 0 :(得分:0)
我测试了您的问题(在MacOS Mojave上),并成功单击了Facebook上的“共享”按钮:
解决方案:在代码执行click_Share
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_1b_2']/div[2]/div/div[2]/div/span[3]/a/text()"))).click()
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_1b_2']/div[2]/div/div[2]/div/span[3]/a/text()"))).click()
我认为问题可能是一些与Facebook相关的一次性问题。在大多数网站上,只需点击一下即可点击元素。
您也可能抓错了XPATH,请替换代码行:
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_1b_2']/div[2]/div/div[2]/div/span[3]/a/text()"))).click()
有这行:
click_Share=WebDriverWait( driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='u_g_2']/div[2]/div/div[2]/div/span[3]"))).click()
每当用户上传图像时,Facebook可能会动态创建新的XPATH,因此,如果CSS_SELECTOR是静态的位置按钮,则应使用该方法而不是XPATH的按钮
答案 1 :(得分:0)
尝试使用javascriptExecutor
shareButton= driver.find_element_by_xpath("//your/xpath/here//")
driver.execute_script("arguments[0].click();", shareButton)