Python-Selenium无法在Eastleigh网站上按下按钮

时间:2018-09-24 13:41:01

标签: python-3.x selenium

我已经在Selenium中制作了一个脚本,该脚本应该可以访问Eastleigh网站,它单击上周日期中的“高级”类型,然后按“搜索”所有这一切,除了不按“搜索”按钮之外,为什么?网站中的名称变量称为“提交”,这是我的代码正在寻找的内容,但出现错误:

Traceback (most recent call last):
  File "C:\Users\Goten\Desktop\code\main.py", line 17, in <module>
    import urls.eastleigh
  File "C:\Users\Goten\Desktop\code\urls\eastleigh.py", line 48, in <module>
    driver.find_element_by_name("submit").click()
  File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Goten\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64)

代码:

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:\Users\Goten\Desktop\chromedriver.exe")
driver.get(url)

time.sleep(3)

driver.find_element_by_id("341:0__item").click()

time.sleep(3)

driver.find_element_by_id("728:0").click()
driver.find_element_by_id("728:0").send_keys(startDate)

driver.find_element_by_id("744:0").click()
driver.find_element_by_id("744:0").send_keys(stopDate)

time.sleep(3)

driver.find_element_by_name("submit").click()

time.sleep(3)

result = []

#driver.find_element_by_xpath("//*[text()='View More']").click()
driver.find_element_by_xpath("//span[text()='View More']/ancestor::a[@class='slds-size--1-of-1 slds-text-align--right']")

elements = driver.find_elements_by_css_selector(".slds-tile cLightningArticle a")
links = [link.get_attribute("href") for link in elements]

result.extend(links)

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
        newDiv = div.replace("Reference ", "").replace("Alternative ", "").replace("Application Validated ", "").replace("Address ", "").replace("Proposal ", "").replace("Status ", "").replace("Decision ", "").replace("Issued Date ", "").replace("Appeal ", "")
        log = open("log.txt", "a")
        log.write(newDiv + "\n")
    log.write("\n")

#driver.close()

1 个答案:

答案 0 :(得分:1)

在DOM中有两个名称为“ submit”的按钮标签。您的目标按钮是第二个按钮。确定“搜索”按钮后,尝试使用第二个索引。

driver.find_element_by_xpath("(//button[@name='submit'])[2]")

enter image description here