比较Python Selenium中的类。

huangapple go评论108阅读模式
英文:

Comparing classes in Python Selenium

问题

我是Python新手,做一些练习时,我需要比较一个HTML网站中的两个类。也就是说,我有同一个项目,一个按钮,在未激活时具有一个类名,而在激活时具有另一个类名。
这是我试图做的示例:

  1. item_class1 = 类名1
  2. item_class2 = 类名2
  3. if webdriver.find_element_by_xpath('button').get_attribute('class') == item_class1:
  4. 做某事
  5. elif webdriver.find_element_by_xpath('button').get_attribute('class') == item_class2:
  6. 做其他事情

.get_attribute('class') 在Python中可以工作,类似于Ruby中的 .class

英文:

I'm new to python, and while doing some exercises, I needed to compare 2 classes in an html site. I.E. I have the same item, a button, which has a class name when it isn't active, and another one when is active.
Here's an example of what I'm trying to do:

  1. item_class1 = Class name
  2. item_class2 = Class name
  3. if webdriver.find_element_by_xpath('button').class == item_class1
  4. DoSomething
  5. elif webdriver.find_element_by_xpath('button').class == item_class
  6. DoSomethingElse

The .class works on Ruby, not on Python, there's a function that works like that?

答案1

得分: 2

Use get_attribute('class')

  1. item_class1 = '类名1'
  2. item_class2 = '类名2'
  3. if webdriver.find_element_by_xpath('//button').get_attribute('class') == item_class1:
  4. print("某事")
  5. elif webdriver.find_element_by_xpath('//button').get_attribute('class') == item_class2:
  6. print("另一些事")
英文:

Use get_attribute('class')

  1. item_class1 = 'Class name'
  2. item_class2 = 'Class name'
  3. if webdriver.find_element_by_xpath('//button').get_attribute('class') == item_class1 :
  4. print("something")
  5. elif webdriver.find_element_by_xpath('//button').get_attribute('class') == item_class2 :
  6. print("some other thing")

huangapple
  • 本文由 发表于 2020年1月3日 19:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定