I have already asked a question similar to this one but it is different.
Still can't understand why the find_elements
aren't working like in the selenium documents :(.
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()
I am trying to click a link which has the type=button
but the tag is <a>
.
Now it worked the very first time but not any more.
This is the error:
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 code:
<div class="pull-right edit">
<a id="editAdLink-225585118" rel="nofollow" class="btn icon-icon_bearbeiten status-expired" title="Bearbeiten" onclick="IADLIB.tealium.trackClickEvent({"tmsData":{"user_role_id":"0","environment":"web","user_email":"firstname.lastname%40live.co.uk","user_is_c2c":"true","page_type":"My_Ads","user_id":"29268370","user_name":"firstname","event_name":"my_ads","user_gender":"0"}}, "N", "my_ads_edit")
" href="/iad/myprofile/editad?adId=225585118&page=6" type="button">
</a>
</div>
答案 0 :(得分:3)
根据您共享的HTML
点击 link
,您可以使用以下代码行:
driver.find_element_by_xpath("//a[@class='btn icon-icon_bearbeiten status-expired' and starts-with(@id,'editAdLink-')]").click()