如何为PrimeFaces编写Python selenium测试?

如何为PrimeFaces编写Python selenium测试?,python,selenium,primefaces,Python,Selenium,Primefaces,我试图编写一个Selenium测试,但问题是我已经了解到页面是用PrimeFaces生成的,因此元素ID会不时地随机变化。不使用IDs不是很可靠。我能做些什么吗?没有有意义的稳定ID不是问题,因为在页面上总是有其他方法来定位元素。仅举几个选项: 部分id与XPath或CSS匹配,例如: # contains driver.find_element_by_css_selector("span[id*=customer]") driver.find_element_by_xpath("//s

我试图编写一个Selenium测试,但问题是我已经了解到页面是用PrimeFaces生成的,因此元素ID会不时地随机变化。不使用IDs不是很可靠。我能做些什么吗?

没有有意义的稳定ID不是问题,因为在页面上总是有其他方法来定位元素。仅举几个选项:

  • 部分id与XPath或CSS匹配,例如:

     # contains
     driver.find_element_by_css_selector("span[id*=customer]")
     driver.find_element_by_xpath("//span[contains(@id, 'customer')]")
    
     # starts with
     driver.find_element_by_css_selector("span[id^=customer]")
     driver.find_element_by_xpath("//span[starts-with(@id, 'customer')]")
    
     # ends with
     driver.find_element_by_css_selector("span[id$=customer]")
    
  • 引用/讲述有关数据类型的一些有价值信息的类(“面向数据的定位器”):

  • 从标签横向移动:

     # <label>Price</label><span id="65123safg12">10.00</span>
     driver.find_element_by_xpath("//label[.='Price']/following-sibling::span")
    
当然,你可以发挥创意,将它们结合起来。还有更多:

还有一个相关的线程,在选择一个方法来定位页面上的元素时,它会回顾最佳实践:


您不能在xhtml源代码中为JSF组件分配id吗?因此,这通常意味着如果您看到“jidt_47”等内容,开发人员没有为页面上的所有元素分配适当的id。在我们所有的PrimeFaces应用程序中,我们都可以分配真实的id,这样它们就不会像您看到的那样自动生成。有什么方法可以让开发者更新应用程序以分配正确的id吗?我没有访问代码的权限。我可能会尝试与开发人员交谈,但我怀疑我能否让他们更改id。谢谢,我只使用了基本的选择器,如按id查找元素、按xpath查找元素、按css查找元素、按文本查找元素等等。我不知道变量和组合,我第一次使用了//。我会查一下它的用途。
 # <label>Price</label><span id="65123safg12">10.00</span>
 driver.find_element_by_xpath("//label[.='Price']/following-sibling::span")
 driver.find_element_by_link_text("Information")
 driver.find_element_by_partial_link_text("more")