Selenium Command

前端 未结 4 1280
逝去的感伤
逝去的感伤 2020-12-22 06:38

When we use selenium command at that time command not find and attribute not get? See below command.



        
                      
相关标签:
4条回答
  • 2020-12-22 06:42

    The Xpath you have used in Invalid.

    You can use xpath as follows and through this you can fins xpath of any object - just need to study the concept:

    Google

    Here as we can see we want to search Google Search just by writing its xpath in console So to find the Google Search button we have to write xpath like this

    //span[@id='gbqfsa']
    

    Once we hit enter it would bring

    [ gbqfsa">​Google Search​​ ],

    It shows that xpath for Google Search Button is correctly written

    Now suppose we want to search Google Search button if we are just familiar that id attributes start with gbqfs

    then we have to use function starts-with like this

    //span[starts-with(@id,'gbqfs')]
    

    and once when we hit enter on console it would reflect two button one is Google Searchand Second one is I’m Feeling Lucky

    [
    gbqfsa">​Google Search​​
    ,
    ​I'm Feeling Lucky​​
    ]
    

    So to find out the Google Search uniquely we need to complete id attribute to gbqfsa

    “//span[starts-with(@id,'gbqfsa')]
    

    and hit to enter and now it would reflect only

    [
    ​Google Search​​
    ],
    

    It proves that we have written right xpath for Google Search

    In the same fashion we can use Contains function to find the Google Search button like this here I have taken fsa from gbqfsa //span[contains(@id,'fsa')]

    hit enter and hopefully it will return

    [
    ​Google Search​​
    ],
    

    if there are multiple attributes then we can use: //span[contains(@id,'fsa') and contains(@class, 'xyz')] hit enter and hopefully it will return

    Information Source: Sumit Mittal Blog

    0 讨论(0)
  • 2020-12-22 06:44

    Xpath //div[2]@class central-featured is invalid. Try changing it to //div[@class='central-featured']/@class if you mean to select a class. You could also use assertElementPresent function instead of selecting attribute, if the whole point is to check that element exists, i.e.: <tr><td>assertElementPresent</td><td>xpath=//div[@class='central-featured']</td><td></td></tr> Much simpler that way.

    0 讨论(0)
  • 2020-12-22 06:46

    You can use CssSelector as below

    webDriver.findElements(By.cssSelector("div.central-featured")) // for more than 1 elements with same class
    
    webDriver.findElement(By.cssSelector("div.central-featured")) // for 1 element
    
    0 讨论(0)
  • 2020-12-22 06:47
    1. Use xPaths in this case.
    2. Use google chrome's built in developer tool for this
    3. Place your cursor on the element
    4. Press Ctrl+Shift+C
    5. Click the Element
    6. That clicked Element's code is highlighted in the short window on the bottom
    7. Right-Click on highlighted code
    8. Select Copy > Copy XPath

    Here it is you have copied the xPath for that specific element. This is shown in the image:

    Click to see how to copy xPath

    0 讨论(0)
提交回复
热议问题
open