Python - Selenium 无法单击 href="javascript:void(0);"



每当我尝试抓取Eastleigh网站时,它都会做所有需要做的事情。 它转到URL,自动单击"高级设置" 在决策日期写入单击搜索并获取所有href链接,但是当它尝试访问它们时,它失败了...为什么?它需要做的就是点击href链接,但它不会 ;-;

有人可以帮助解决这个问题吗?

法典:

import sys
import time
import config
import datetime
from selenium import webdriver
print("1. Custom Date")
print("2. Last Week")
choice = input("#: ")
if choice == "1":
print("Start Example: 1/8/2018")
startDate = input("Start Date: ")
print("Stop Example: 30/8/2018")
stopDate = input("Stop Date: ")
elif choice == "2":
sd = str(datetime.datetime.today().day) # Gets day of the month
sm = str(datetime.datetime.today().month) # Gets month of the year
sy = str(datetime.datetime.today().year) # Gets year
nsd = int(sd) # Turns string variable "sd" into an integer
startDate = "%s/%s/%s" % (nsd-7, sm, sy) # Makes a new date string. Minus 7 off of the original date to go back 1 week
stopDate = "%s/%s/%s" % (nsd-1, sm, sy) # Makes a new date string. Minus 1 off of the original date, (Minusing 1 was Steve's idea, not mine.)
else:
print("This is not a choice.")
print("Press Enter to exit...")
input("")
sys.exit()
url = "https://planning.eastleigh.gov.uk/s/public-register"
driver = webdriver.Chrome(executable_path=r"C:UsersGotenDesktopchromedriver.exe")
driver.get(url)
time.sleep(2)
driver.find_element_by_xpath("(//li[@class='slds-tabs_default__item'])[1]").click()
driver.find_element_by_id("728:0").click() # This changes for some reason... I cannot quite find a way to make it stay the same...
driver.find_element_by_id("728:0").send_keys(startDate)
driver.find_element_by_id("744:0").click() # This also changes
driver.find_element_by_id("744:0").send_keys(stopDate)
driver.find_element_by_xpath("(//button[@name='submit'])[2]").click()
time.sleep(2)
driver.find_element_by_xpath("//*[text()='View More']").click()
result = []
elements = driver.find_elements_by_css_selector(".slds-truncate a")
links = [link.get_attribute("href") for link in elements]
result.extend(links)
print(result)
for link in result:
result.remove(link)
driver.get(link)
for i in range(1):
div = driver.find_element_by_id("slds-form-element__group").text
log = open("log.txt", "a")
log.write(div + "n")
log.write("n")
#driver.close()

输出:

[8520:11412:0926/001704.487:ERROR:install_util.cc(603)] Failed to read HKLMSOFTWAREPoliciesGoogleChromeMachineLevelUserCloudPolicyEnrollmentToken: The system cannot find the file specified. (0x2)
DevTools listening on ws://127.0.0.1:59340/devtools/browser/bc325d32-310c-43e6-b8cf-dcfceaebf5a5
['javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);', 'javascript:void(0);']
Traceback (most recent call last):
File "code/main.py", line 17, in <module>
import urls.eastleigh
File "C:UsersGotenAnaconda3codeurlseastleigh.py", line 61, in <module>
driver.get(link)
File "C:UsersGotenAnaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 332, in get
self.execute(Command.GET, {'url': url})
File "C:UsersGotenAnaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:UsersGotenAnaconda3libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unsupported protocol
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)

事实上,你不能指望这些id,因为它们是动态生成的,正如你自己所指出的。 黑客解决方案是:

...
inputs = driver.find_elements_by_class_name(" input")
received_from_index = 1
received_to_index = 2
decision_from_index = 3
decision_to_index = 4
received_from = inputs[received_from_index]
received_to = inputs[received_to_index]
received_from.clear()
received_from.send_keys(startDate)
received_to.clear()
received_to.send_keys(stopDate)

这将填写字段(不确定您是否需要填写所有字段(。 之后,您的脚本将提交并正确获取结果页面。

您需要修改代码的这一部分:

for link in result:
result.remove(link)
driver.get(link)
...

对于您需要的第二部分:

...
driver.find_element_by_xpath("//*[text()='View More']").click()
links = driver.find_elements_by_xpath("""//*[@id="arcusbuilt__PApplication__c"]/div/div/h4/a""")
print "Total of planning applications : ", len(links)

for link_index in range(1, len(links) +1):
result_link = driver.find_element_by_xpath("""//*[@id="arcusbuilt__PApplication__c"]/div[%d]/div/h4/a"""%link_index)
result_link.click()
print "visiting link %d"%link_index
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.NAME, "BackButton")))
time.sleep(3)
#DO WHAT YOU NEED HERE...
back_btn = driver.find_element_by_name("BackButton")
back_btn.click()

祝你好运!

最新更新