Python +硒.找不到元素 [英] Python+Selenium. Can't locate element

查看:75
本文介绍了Python +硒.找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Python和Selenium实现了脚本来点击广告. 但是现在此脚本无法正常工作. 在页面上找不到元素. 请帮助我更正脚本.谢谢!

I've implemented the script using Python and selenium to click on the ads. But now this script is not working. Unable to find element on the page. Please help me to correct the script. Thank you!

from selenium import webdriver
import time
browser=webdriver.Firefox()
browser.get('http://rutracker.ignn.ru')
browser.switch_to_frame(browser.find_element_by_xpath('//iframe[starts-with(@class,"tblock_")]'))
browser.find_element_by_xpath("//a[contains(@href,'tmozs.com')]").click()
time.sleep(10)
browser.quit()

推荐答案

以下是您的问题的答案:

Here is the Answer to your Question:

您可以使用以下代码块点击左上角的广告:

You can use the below code block to click on the ad at the top left corner:

我专门使用time.sleep()来减少准备此解决方案的时间.所有time.sleep()都可以替换为 Explicit Wait

I have exclusively used time.sleep() to reduce the time in preparing this solution. All the time.sleep() can be replaced by Explicit Wait

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get('http://rutracker.ignn.ru')
time.sleep(5)
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'tmozs.com/jsonp.php')]"))
time.sleep(5)
driver.find_element_by_xpath("//div[@class='feed-inner']/div[@class='feed-cell'][1]/a[@class='feed-cell-inner']/div[@class='teaser']/table[@class='teaser-inner']//img[contains(@src,'in.ogoyj.com')]").click()
time.sleep(5)
driver.quit()

让我知道这是否回答了您的问题.

Let me know if this Answers your Question.

这篇关于Python +硒.找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆