Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用bs4、python和selenium刮取标题_Python_Selenium_Selenium Webdriver_Web Scraping_Beautifulsoup - Fatal编程技术网

无法使用bs4、python和selenium刮取标题

无法使用bs4、python和selenium刮取标题,python,selenium,selenium-webdriver,web-scraping,beautifulsoup,Python,Selenium,Selenium Webdriver,Web Scraping,Beautifulsoup,我正在尝试使用以下代码从该链接的“福利”选项卡中删除标题: 代码 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.webdriv

我正在尝试使用以下代码从该链接的“福利”选项卡中删除标题:

代码

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.firefox.options import Options
from bs4 import BeautifulSoup


options = Options()
options.headless = True
options.set_preference("dom.webdriver.enabled", False)
driver = webdriver.Firefox(options=options, executable_path="C:\\Users\\Hari\\Downloads\\geckodriver.exe")
driver.get("https://www.yesbank.in/business-banking/cards/credit-cards/yes-first-business-card")

d0 = []
element = WebDriverWait(driver, 10).until(
    EC.title_contains('YES'))
soup_1 = BeautifulSoup(driver.page_source, 'lxml')

for x in soup_1.select('u strong , ul:nth-child(21) p > strong , ul:nth-child(19) strong , ul:nth-child(17) strong , ul:nth-child(7) strong , ul:nth-child(5) p > strong , ul:nth-child(10) strong , ul~ ul li+ li strong , ul:nth-child(13) strong , ul:nth-child(3) strong'):
    d0.append(x.get_text())

print(d0)

driver.quit()


但我得到的错误如下:

Traceback (most recent call last):
  File "C:\Users\Hari\PycharmProjects\Card_Prj\buffer.py", line 43, in <module>
    element = WebDriverWait(driver, 10).until(
  File "C:\Users\Hari\PycharmProjects\Card_Prj\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

回溯(最近一次呼叫最后一次):
文件“C:\Users\Hari\PycharmProjects\Card\u Prj\buffer.py”,第43行,在
元素=WebDriverWait(驱动程序,10)。直到(
文件“C:\Users\Hari\PycharmProjects\Card\u Prj\venv\lib\site packages\selenium\webdriver\support\wait.py”,第80行,直到
引发TimeoutException(消息、屏幕、堆栈跟踪)
selenium.common.Exception.TimeoutException:消息:
我的主要目标是打印“d0”列表

我尝试了很多方法来解决这个问题,但都没有成功


请帮我解决这个问题。

请用try-catch将其包装起来

try:
    element = WebDriverWait(driver, 10).until(EC.title_contains('YES'))
except:
    print('Not there')
更好的方法是处理准确的错误

from selenium.common.exceptions import TimeoutException

try:
    element = WebDriverWait(driver, 10).until(EC.title_contains('YES'))
except TimeoutException as ex:
    print(str(ex))

也是“是”不等于“是”

我通过更改此选项使其正常工作:

EC.title_contains('YES'))
为此:

EC.title_contains('Yes'))