GVKun编程网logo

检查Python Selenium是否存在元素(python怎么检查)

22

如果您想了解检查PythonSelenium是否存在元素的相关知识,那么本文是一篇不可错过的文章,我们将对python怎么检查进行全面详尽的解释,并且为您提供关于java+selenium,pytho

如果您想了解检查Python Selenium是否存在元素的相关知识,那么本文是一篇不可错过的文章,我们将对python怎么检查进行全面详尽的解释,并且为您提供关于java+selenium, python+selenium浏览界面,滑动页面、Python - 仅在 Selenium 中不存在的元素、python selenium 判断元素是否可见、python selenium 判断元素是否存在的问题的有价值的信息。

本文目录一览:

检查Python Selenium是否存在元素(python怎么检查)

检查Python Selenium是否存在元素(python怎么检查)

我有一个问题-我正在使用Selenium(firefox)Web驱动程序打开网页,单击一些链接等,然后捕获屏幕截图。

我的脚本可以从CLI正常运行,但是通过cronjob运行时,它并没有通过第一个find_element()测试。我需要添加一些调试,或一些帮助我弄清为什么失败的东西。

基本上,在进入登录页面之前,我必须单击“登录”锚点。元素的构造为:

<arel="nofollow" href="/login.jsp?destination=/secure/Dash.jspa">log in</a>

我正在通过LINK_TEXT方法使用find_element:

login = driver.find_element(By.LINK_TEXT, "log in").click()

我有点Python Noob,所以我在和这种语言作斗争…

A)如何检查python是否实际拾取了链接?我应该使用try / catch块吗?

B)是否有比LINK_TEXT更好/更可靠的方式来定位DOM元素?例如,在JQuery中,可以使用更具体的选择器$(’a.lnk:contains(log
in)’)。do_something();


我已经解决了主要问题,这只是手指上的麻烦-我用错误的参数调用脚本-简单的错误。

我仍然想要一些有关如何检查元素是否存在的指针。同样,使用隐式/显式Waits的示例/解释,而不是使用糟糕的time.sleep()调用。

干杯,ns

答案1

小编典典

A)是的。检查元素是否存在的最简单方法是find_element在内简单调用try/catch

B)是的,出于两个原因,我总是尝试不使用元素文本来标识元素:

  1. 文本更可能被更改;并且
  2. 如果这对您很重要,则您将无法针对本地化版本运行测试。

解决方案之一:

  1. 您可以使用xpath查找具有ID或其他唯一标识符的父元素或祖先元素,然后找到与或相匹配的子代/后代。
  2. 您可以请求链接本身的ID或名称或其他唯一标识符。

对于后续问题,使用try/catch可以知道如何确定元素是否存在,可以在这里找到很好的等待示例:http
:
//seleniumhq.org/docs/04_webdriver_advanced.html

java+selenium, python+selenium浏览界面,滑动页面

java+selenium, python+selenium浏览界面,滑动页面

用到selenium的时候,有时需要滑动屏幕,模拟浏览,这时候就可以用到下边的代码了:

首先是java的selenium滑屏代码(不知道为啥后边就乱了排版,自己调整下就好~)

private static Random random = new Random();
private void pretendScrolling(JavascriptExecutor js, int sum) throws InterruptedException {
sum--;
int step;
int length;
switch (sum) {
case 0:
step = random.nextInt(30) + 1 + 50;
length = random.nextInt(3000) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, 0, step, length, false);

smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, length, step, 8000, true);

step = random.nextInt(20) + 1 + 80;
smoothlyScrolling(js, 0, step, 8000, false);

length = random.nextInt(2000) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, 0, 20, length, false);
break;
case 1:
step = random.nextInt(10) + 1 + 40;
smoothlyScrolling(js, 0, step, 6000, true);
Thread.sleep(3000);
smoothlyScrolling(js, 0, step, 6000, false);

length = random.nextInt(1500) + 1 + 1000;
step = random.nextInt(10) + 1 + 40;
Thread.sleep(3000);
smoothlyScrolling(js, 0, step, length, true);
Thread.sleep(5000);
step = random.nextInt(10) + 1 + 40;
smoothlyScrolling(js, length, step, 7000, true);
Thread.sleep(3000);

step = random.nextInt(20) + 1 + 80;
smoothlyScrolling(js, 0, step, 8000, false);
Thread.sleep(3000);
break;
case 2:
step = random.nextInt(10) + 1 + 30;
length = random.nextInt(1300) + 1 + 1000;
smoothlyScrolling(js, 0, step, length, true); Thread.sleep(8000); step = random.nextInt(10) + 1 + 20; smoothlyScrolling(js, length, step, 6000, true); Thread.sleep(3000); length = random.nextInt(1500) + 1 + 1500; step = random.nextInt(20) + 1 + 40; smoothlyScrolling(js, length, step, 6000, false); Thread.sleep(3000); step = random.nextInt(30) + 1 + 70;
smoothlyScrolling(js, length, step, 6000, true);
Thread.sleep(4000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 6000, false);
Thread.sleep(3000);
break;
case 3:
step = random.nextInt(10) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(3000);
step = random.nextInt(30) + 1 + 20;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(3000);
break;
case 4:
step = random.nextInt(10) + 1 + 50;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(3000);
length = random.nextInt(1500) + 1 + 2000;
step = random.nextInt(25) + 1 + 20;
smoothlyScrolling(js, length, step, 7000, false);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, length, step, 7000, true);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 40;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(2000);
break;
case 5:
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 2500, true);
Thread.sleep(3000);
step = random.nextInt(30) + 1 + 40;
smoothlyScrolling(js, 0, step, 2500, false);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(6000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(2000);
break;
default:
case 6:
step = random.nextInt(30) + 1 + 30;
length = random.nextInt(1500) + 1 + 2500;
smoothlyScrolling(js, 0, step, length, true);
step = random.nextInt(30) + 1 + 70;
smoothlyScrolling(js, 0, step, length, false);
step = random.nextInt(20) + 1 + 40;
smoothlyScrolling(js, 0, step, 7000, true);
step = random.nextInt(25) + 1 + 20;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(3000);
step = random.nextInt(25) + 1 + 20;
length = random.nextInt(1500) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
Thread.sleep(3000);
step = random.nextInt(15) + 1 + 30;
smoothlyScrolling(js, 0, step, length, false);
break;
}
}
private void smoothlyScrolling(JavascriptExecutor js, int floor, int step, int celling, boolean direction) {
if (direction) {
for (int i = floor; i <= celling; i += step) {
js.executeScript("scrollTo(0," + i + ")");
}
} else {
for (int i = celling; i >= floor; i -= step) {
js.executeScript("scrollTo(0," + i + ")");
}
}
}

用法如下:



两个参数,其中js表示的是driver, sum是一个随机数,表示随机的从7中浏览方式之间选一种


再然后是在python selenium中的应用,我专门写了一个类出来存放浏览界面的代码,然后在需要用到它的地方导入,引用
import random
import time


class PretendScrolling:
@staticmethod
def pretend_scrolling(driver, sum):
step = 1
length = 1
if sum == 1:
step = random.randint(0, 30) + 1 + 50
length = random.randint(0, 3000) + 1 + 3000
smoothlyScrolling(driver, 0, step, length, True)
smoothlyScrolling(driver, 0, step, length, False)

smoothlyScrolling(driver, 0, step, length, True)
smoothlyScrolling(driver, length, step, 8000, False)
step = random.randint(0, 20)+ 1+80
smoothlyScrolling(driver, 0 ,step, 8000, False)

length = random.randint(0, 2000) + 1 + 3000
smoothlyScrolling(driver, 0 , step, length, True)
smoothlyScrolling(driver, 0 , 20, length, False)
elif sum == 2:
step = random.randint(0, 10) + 1 + 40
smoothlyScrolling(driver, 0, step, 6000, True)
time.sleep(3)
smoothlyScrolling(driver, 0, step, 6000, False)

length = random.randint(0, 1500) + 1 + 1000
step = random.randint(0, 10) + 1 + 40
time.sleep(3)
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(5)
step = random.randint(0, 10)+1+40
smoothlyScrolling(driver, length, step, 7000, True)
time.sleep(3)

step = random.randint(0, 20)+1+80
smoothlyScrolling(driver, 0, step, 8000, False)
time.sleep(3)
elif sum == 3:
step = random.randint(0, 10) + 1 + 30
length = random.randint(0, 1300) + 1 + 1000
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(8)
step = random.randint(0, 10) + 1 + 20
smoothlyScrolling(driver, length, step, 6000, True)
time.sleep(3)
length = random.randint(0, 1500) + 1 + 1500
step = random.randint(0, 20) + 1 + 40
smoothlyScrolling(driver, length, step, 6000, False)
time.sleep(3)
step = random.randint(0, 30) + 1 + 70
smoothlyScrolling(driver, length, step, 6000, True)
time.sleep(4)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 6000, False)
time.sleep(3)
elif sum == 4:
step = random.randint(0, 10) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(3)
step = random.randint(0, 30) + 1 + 20
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(3)
elif sum == 5:
step = random.randint(0, 10) + 1 + 50
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(3)
length = random.randint(0, 1500) + 1 + 2000
step = random.randint(0, 25) + 1 + 20
smoothlyScrolling(driver, length, step, 7000, False)
time.sleep(3)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, length, step, 7000, True) time.sleep(3) step = random.randint(0, 20) + 1 + 40 smoothlyScrolling(driver, 0, step, 7000, False) time.sleep(2) elif sum == 6: step = random.randint(0, 20) + 1 + 30 smoothlyScrolling(driver, 0, step, 2500, True) time.sleep(3)
step = random.randint(0, 30) + 1 + 40
smoothlyScrolling(driver, 0, step, 2500, False)
time.sleep(3)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(6)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(2)
elif sum == 7:
step = random.randint(0, 30) + 1 + 30
length = random.randint(0, 1500) + 1 + 2500
smoothlyScrolling(driver, 0, step, length, True)
step = random.randint(0, 30) + 1 + 70
smoothlyScrolling(driver, 0, step, length, False)
step = random.randint(0, 20) + 1 + 40
smoothlyScrolling(driver, 0, step, 7000, True)
step = random.randint(0, 25) + 1 + 20
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(3)
step = random.randint(0, 25) + 1 + 20
length = random.randint(0, 1500) + 1 + 3000
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(3)
step = random.randint(0, 15) + 1 + 30
smoothlyScrolling(driver, 0, step, length, False)
else:
pass



#(不知道为啥会乱排版,这是另一个方法)def smoothlyScrolling(driver, floor, step, celling, direction):
if direction is True:
i = floor
while i <= celling:
i += step
jscript = "window.scrollTo(1, {height_i})".format(height_i=i)
driver.execute_script(jscript)
else:
i = celling
while i >= floor:
i -= step
jscript = "window.scrollTo(1, {height_i})".format(height_i=i)
driver.execute_script(jscript)



 







在python的其它类中引用如下:
首先先在相关类中引入滚动界面的类

 

然后调用:
以上就是selenium里边滑动界面的一组方法了,支持java和python.
 





因为图片好像没有加载出来,所以再放一下.然后也不知道为啥排版会乱,要用的话自己调整一下即可.

 



 

 


  

 

 

Python - 仅在 Selenium 中不存在的元素

Python - 仅在 Selenium 中不存在的元素

如何解决Python - 仅在 Selenium 中不存在的元素?

我需要引入自动验证码来创建帐户生成器。第一张图片来自我的 Chrome,第二张图片来自 selenium Chrome。问题是第一张图有个叫sitekey的元素,第二张图不存在。你能解决这个问题吗?

first

second

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

python selenium 判断元素是否可见

python selenium 判断元素是否可见

1 #在元素定位二次封装的基础上,封装判断元素是否可见,找到元素时返回True,找不到元素的时候抛出异常,返回False
2 def isElementPresent(self,locate_type,value):
3     try:
4        self.findElement(locate_type,value)
5        return True
6     except NoSuchElementException as e:
7         # print(e)
8         return False

 

python selenium 判断元素是否存在的问题

python selenium 判断元素是否存在的问题

爬虫的时候经常用到这个,找到了一个比较好用的方法

原文链接:http://blog.csdn.net/u012189659/article/details/36391837

背景:selenium+python 处于学习阶段,功能实现之后开始整理之前写的代码,突然发现一个功能没有做判断,比如,点击 “请登录”,需要判断一下这个弹出框到底弹了没有,这样就需要判断某个元素是否存在,使用 is_displayed 函数只能用于该元素存在去判断此元素是否出现,而不能判断某个元素是否存在,所以就自己写了个方法

 

 

代码:

复制代码
from selenium import webdriver
import unittest
from selenium.webdriver.common.action_chains import ActionChains
class test1(unittest.TestCase):
    
    def setUp(self):
        self.driver=webdriver.Firefox()
        self.baseurl="http://www.xebest.com"
#        self.driver.maximize_window()
        
        
        
        
        
    def dengLu(self):
        browser=self.driver
        
        browser.get(self.baseurl)
        
        browser.find_element_by_link_text(u"请登录").click()
        #调用isElementExist方法,判断元素是否存在
        flag=test1.isElementExist(self, "div.popup-content")         
        
        if flag:
            
            browser.find_element_by_id("userName").send_keys("w74581@163.com")
            browser.find_element_by_id("password").send_keys("w123456")
            browser.find_element_by_id("imgLogin").click()
            print(browser.switch_to_alert().text)
            browser.switch_to_alert().accept()
            
            
        else:
            print("没有弹框")
        
        
        
        
#   该方法用来确认元素是否存在,如果存在返回flag=true,否则返回false        
    def isElementExist(self,element):
        flag=True
        browser=self.driver
        try:
            browser.find_element_by_css_selector(element)
            return flag
        
        except:
            flag=False
            return flag
复制代码

关于检查Python Selenium是否存在元素python怎么检查的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于java+selenium, python+selenium浏览界面,滑动页面、Python - 仅在 Selenium 中不存在的元素、python selenium 判断元素是否可见、python selenium 判断元素是否存在的问题的相关知识,请在本站寻找。

本文标签: