Automated testing classic interview questions - can not locate the element

insert image description here


1. Element positioning

1. How many positioning methods are there for Selenium/Appium? respectively?

selenium:

xpath、css、id、name、class_name、tag_name、link_text、partail_link_text

Appium:

xpath, css, id (find elements through identity id, corresponding to resource-id), accessibility_id (find elements through accessibility_id, corresponding to content-desc)

2. How to locate the parent element through the child element

Get the parent node of the current node: //input[@name="wd"]/..
Get the grandpa node of the current node://input[@name="wd"]/../..

Second, the element cannot be positioned

1. What is the reason why the element cannot be located?

Reason: The element positioning expression is written incorrectly (maybe the word or format is wrongly written).
Solution: Check the word, element positioning expression.

Reason: The element location is wrongly written, and NoSushElementException is reported (search for the corresponding element in elements, and if it is not found, it proves that the location is wrongly written) Solution
: Check and write a correct location

Reason: Because the page loads quickly, the find_element operation is executed before the element is loaded.
Debug ideas: 1. Add a mandatory wait, and then execute the find operation after confirming that the page is loaded. If the use case passes at this time, it means that there is no problem with element positioning . Otherwise it is another reason.
Solution: use implicit wait and explicit wait

Reason: Because the id is dynamic and changes all the time, using the ID to locate it will be very unstable at this time. Solution
: Check whether the id of the element used each time is consistent; use other positioning methods, relative to xpath or css position

Reason: After excluding the above situations, there is still the possibility of iframe
debugging Ideas: 1. Check the elements page of the browser and enter iframe to confirm whether the page contains iframe.
2. Then hover the mouse over the iframe element to check whether the element to be located is In the iframe page, if it is, then the operation of the iframe needs to be switched before positioning. driver. switch_to. frame()

Reason: A new window is opened, and the element of the new window is located.
Debug idea: Check whether a new page is opened.
Solution: If you want to locate the element of the new window, you need to switch to the corresponding page before locating
windows=driver .window_handles
driver.switch_to.window(windows[-1])

2. How to locate dynamic elements

Check whether the id of the element used each time is consistent, and determine whether it is a dynamic element;
if it is a dynamic element, use other positioning methods, relative positioning of xpath or css

3. Some elements are loaded on the page, but you cannot locate them, how to solve

Same as 1

3. Element operation

1. An element is obviously located, but the click is invalid (no error is reported), how to solve it?

Reason: Asynchronous loading of js causes the click event to be invalid.
Solution: Encapsulate an explicit wait by yourself, and click the button in a loop until it takes effect <explicit wait>

2. How to locate and operate hidden elements in selenium?

Reason: Hidden elements can be directly located, but cannot be clicked or interacted with directly.
Solution: Use js to perform interactive operations

driver.excute_script("document.querySelector('#kw').value='python' "): Modify text box text information
driver.excute_script("document.querySelector('#kw').value"): Get text box Text information
driver.excute_script("document.querySelector('#su').click()"): Click operation
driver.excute_script("document.querySelector('#train_date').value"): Get the time control information, and return out

3. If the product always pops up and the use case cannot be executed, how should it be solved? (unfinished)

In the process of app automation testing, pop-ups from time to time (advertisement pop-ups, upgrade prompts, new message prompts, etc.)
are not bugs (UI interface prompts, warnings)
insert image description here
Solution: Solve the exception by adding a blacklist Processing ; using decorators
to enhance the original function
without changing the logic of the original function
to make the code more concise and easy to
maintain 1001.2014.3001.5501

4. How to get the toast message prompt in the app?

Reason: the toast flashes too fast, it is not easy to locate
the solution: directly through xpath positioning combined with implicit waiting to obtain "special control toast recognition"
appium uses the underlying mechanism of uiautomator to analyze and capture the toast, and put the toast in the control tree, But it is not a control itself.

Method 1: Method 2:
self.driver.find_element(AppiumBy.XPATH,"//*[contains(@text,'popup menu')]")

self.driver.find_element(AppiumBy.XPATH,"//android.widget.Toast[@class='android.widget.Toast']")

4. Whether the element exists on the page

1. How to judge whether an element exists on a page?

Solution: Search for the existence of the element by looking at the current page dom. If the script is running automatically, you should print page_source to know whether the element exists during the running process

Guess you like

Origin blog.csdn.net/YZL40514131/article/details/130517091