Python selenium通过xpath单击元素不起作用

Python selenium通过xpath单击元素不起作用,python,selenium,beautifulsoup,Python,Selenium,Beautifulsoup,我正试图从我们的网页上搜集一些信息,比如标题、地点、联系电话。 我在python中使用了Selenium和BS4库。当我们单击网页中的“显示号码”元素时,网页仅显示联系人号码。 我尝试使用selenium单击,但它不起作用 我的代码(我尝试的内容): import time from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Opti

我正试图从我们的网页上搜集一些信息,比如标题、地点、联系电话。 我在python中使用了Selenium和BS4库。当我们单击网页中的“显示号码”元素时,网页仅显示联系人号码。 我尝试使用selenium单击,但它不起作用

我的代码(我尝试的内容):

import time
from bs4 import BeautifulSoup
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works

#options.add_argument('--disable-gpu')  # maybe needed if running on Windows.
driver = webdriver.Chrome(executable_path='/home/dobuyme/Desktop/chromedriver', options=chrome_options)
print("Loading Page...")
driver.get('https://smartarz.com/furniture/1114/furniture/3304/beds-bed-sets/3315/6443')
time.sleep(5)
element = driver.find_elements_by_xpath("/html/body/app-root/div/app-main/div/ng-component/app-sale-property-detail-page/div/main/div[2]/div[1]/div/app-post-detail-image-gallery/app-gallery/div/app-gallery-thumbnails/div/div")
element.click()
soup = BeautifulSoup(driver.page_source,"html.parser")
title = soup.find("span", {"class": ["_truncate_multilines multiline_truncation"]}).get_text()
app = soup.find("div", {"class": ["_top_row-destop text-left"]}).find("h2").get_text().strip()
driver.quit()
contact = soup.find("div", {"class": ["_user_contact"]}).find("p", {"class": ["_call text-left"]}).get_text()
print(contact)
Loading Page...
Traceback (most recent call last):
  File "/home/dobuyme/Downloads/BS4 Scrapper/Haraj.com/smart.py", line 18, in <module>
    element.click()
AttributeError: 'list' object has no attribute 'click'

我收到的错误:

import time
from bs4 import BeautifulSoup
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works

#options.add_argument('--disable-gpu')  # maybe needed if running on Windows.
driver = webdriver.Chrome(executable_path='/home/dobuyme/Desktop/chromedriver', options=chrome_options)
print("Loading Page...")
driver.get('https://smartarz.com/furniture/1114/furniture/3304/beds-bed-sets/3315/6443')
time.sleep(5)
element = driver.find_elements_by_xpath("/html/body/app-root/div/app-main/div/ng-component/app-sale-property-detail-page/div/main/div[2]/div[1]/div/app-post-detail-image-gallery/app-gallery/div/app-gallery-thumbnails/div/div")
element.click()
soup = BeautifulSoup(driver.page_source,"html.parser")
title = soup.find("span", {"class": ["_truncate_multilines multiline_truncation"]}).get_text()
app = soup.find("div", {"class": ["_top_row-destop text-left"]}).find("h2").get_text().strip()
driver.quit()
contact = soup.find("div", {"class": ["_user_contact"]}).find("p", {"class": ["_call text-left"]}).get_text()
print(contact)
Loading Page...
Traceback (most recent call last):
  File "/home/dobuyme/Downloads/BS4 Scrapper/Haraj.com/smart.py", line 18, in <module>
    element.click()
AttributeError: 'list' object has no attribute 'click'

正在加载页面。。。
回溯(最近一次呼叫最后一次):
文件“/home/dobuyme/Downloads/BS4 scraster/Haraj.com/smart.py”,第18行,在
元素。单击()
AttributeError:“列表”对象没有“单击”属性
我不知道我的代码哪里出了问题。任何人都可以解决这个问题,并帮助我打印联系电话


感谢驱动程序。通过xpath查找元素方法返回web元素列表。
如果您想获取单个web元素并单击它,您应该使用
驱动程序。改为使用\u xpath
方法查找\u元素

element = driver.find_element_by_xpath("/html/body/app-root/div/app-main/div/ng-component/app-sale-property-detail-page/div/main/div[2]/div[1]/div/app-post-detail-image-gallery/app-gallery/div/app-gallery-thumbnails/div/div")
element.click()
而且,你的定位器是坏的。改用这个:

show_number_btn_xpath = '//span[contains(text(),'Show number')]'
driver.find_element_by_xpath(show_number_btn_xpath).click()
UPD
由于它现在在常规模式下工作正常,在headless模式下抛出
元素不可交互
错误,我想您应该添加

chrome_options.add_argument("--start-maximized")
那么,如果你愿意的话

chrome_options.add_argument("--headless")
chrome_options.add_argument("--start-maximized")
这将返回一个元素列表。您可能想要第一个:

element[0].click()

这在没有selenium headless模式的情况下工作。如果我;我尝试启用无头模式,它显示错误“消息:元素不可交互”我想我有个主意。请查看更新的答案