点击链接时出错(<a> tag, type=button) using selenium in python

时间:2017-12-21 15:52:28

标签: python google-chrome selenium selenium-webdriver

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 :(.

  • 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()

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({&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>

1 个答案:

答案 0 :(得分:3)

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

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