这是我的第一篇文章,所以如果有任何错误,请接受我的道歉。
首先,我对python真的很新。我的问题可能很愚蠢,但我试图搜索很多来源,但对我来说没有答案。
我想要做的是随后在同一个网址中点击两个链接。换句话说,第二个链接将在单击第一个链接后显示。
代码运行良好,但没有点击第二个链接。任何建议都非常欢迎。谢谢
我在这里尝试过:
from selenium import webdriver
driver = webdriver.Chrome("C:/Users/Kob/Desktop/Python projects/Chrome webdriver/Chromedriver.exe")
driver.get("http://www2.ops3.moc.go.th/")
Export = driver.find_element_by_link_text("EXPORT")
Export.click()
driver.implicitly_wait(10)
Yearly = driver.find_element_by_link_text("Yearly")
Yearly.click()
这是发生的错误消息
runfile('C:/Users/Kob/Desktop/Python projects/MOC_spyder project/web driver.py', wdir='C:/Users/Kob/Desktop/Python projects/MOC_spyder project')
Traceback (most recent call last):
File "<ipython-input-12-05875762a23a>", line 1, in <module>
runfile('C:/Users/Kob/Desktop/Python projects/MOC_spyder project/web driver.py', wdir='C:/Users/Kob/Desktop/Python projects/MOC_spyder project')
File "c:\users\kob\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "c:\users\kob\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Kob/Desktop/Python projects/MOC_spyder project/web driver.py", line 19, in <module>
Yearly.click()
AttributeError: 'NoneType' object has no attribute 'click'
答案 0 :(得分:0)
要点击链接每年,您必须切换到此链接所在的IFrame“数据”。
因此,在找到“Yearly”链接之前,请添加以下语句:
driver.switch_to.frame("data")
我测试并运行的完整代码:
driver.get("http://www2.ops3.moc.go.th/")
main_window = driver.window_handles[0]
new_window1 = driver.window_handles[1]
new_window2 = driver.window_handles[2]
driver.switch_to.window(new_window1)
driver.close()
driver.switch_to.window(new_window2)
driver.close()
driver.switch_to.window(main_window)
Export = driver.find_element_by_link_text("EXPORT")
Export.click()
driver.switch_to.frame("data")
driver.implicitly_wait(10)
Yearly = driver.find_element_by_link_text("Yearly")
Yearly.click()