gpt4 book ai didi

python - 在 python 中使用 selenium 单击链接( 标记,type=button)时出错

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:41 25 4
gpt4 key购买 nike

我已经问过一个与此类似的问题,但它是不同的。
仍然不明白为什么 find_elements不像 selenium 文档那样工作 :(。

  • Python v3.5.4
  • Selenium v3.8.0
  • PhantomJS v2.1

from bs4 import BeautifulSoup as soup
import requests
import sys
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

input("If ready press 'enter'")

loginurl = 'https://www.willhaben.at/iad/?islogout=true&logoff.y=10'

chrome_options = Options()
chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path="C:\\Users\\laure\\Downloads\\chromedriver_win32\\chromedriver.exe", chrome_options=chrome_options, service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])


USERNAME = 'notgonnagiveit :)'
PASSWORD = 'notgonnagiveit'

driver.get(loginurl)

element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="username"]'))
)

username = driver.find_element_by_xpath('//input[@id="username"]')
password = driver.find_element_by_xpath('//input[@id="password"]')

password.send_keys(PASSWORD)
username.send_keys(USERNAME)

for i in range(input("Number of products to reactivate: ")):
driver.get("https://www.willhaben.at/iad/myprofile/adadministration/showlist?page=6")
driver.find_element_by_xpath("//a[@class='btn icon-icon_bearbeiten status-expired' and starts-with(@id,'editAdLink-')]").click()
driver.quit()

我正在尝试单击具有 type=button 的链接但标签是<a> .
现在它第一次工作了,但以后就不行了。
这是错误:

Traceback (most recent call last):


File "D:\OneDrive\Dokumente\Willhaben\Willhaben.py", line 85, in <module>
editbutton = driver.find_element_by_xpath("//input[@class='btn icon-icon_bearbeiten status-expired'][@type='button']")
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 368, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\laure\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@class='btn icon-icon_bearbeiten status-expired'][@type='button']"}
(Session info: chrome=63.0.3239.84)
(Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.16299 x86_64)

html代码:

<div class="pull-right edit">
<a id="editAdLink-225585118" rel="nofollow" class="btn icon-icon_bearbeiten status-expired" title="Bearbeiten" onclick="IADLIB.tealium.trackClickEvent({&quot;tmsData&quot;:{&quot;user_role_id&quot;:&quot;0&quot;,&quot;environment&quot;:&quot;web&quot;,&quot;user_email&quot;:&quot;firstname.lastname%40live.co.uk&quot;,&quot;user_is_c2c&quot;:&quot;true&quot;,&quot;page_type&quot;:&quot;My_Ads&quot;,&quot;user_id&quot;:&quot;29268370&quot;,&quot;user_name&quot;:&quot;firstname&quot;,&quot;event_name&quot;:&quot;my_ads&quot;,&quot;user_gender&quot;:&quot;0&quot;}}, &quot;N&quot;, &quot;my_ads_edit&quot;)
" href="/iad/myprofile/editad?adId=225585118&amp;page=6" type="button">
</a>
</div>

最佳答案

根据您共享的 HTML 点击 link 您可以使用以下代码行:

driver.find_element_by_xpath("//a[@class='btn icon-icon_bearbeiten status-expired' and starts-with(@id,'editAdLink-')]").click()

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com