Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

How to access to 'rect' type element through Selenium-Python

There is a rect object in the dom:

<rect class="slv-blank" id="id123" height="8.8" stroke-width="1px" width="18.8" x="59.2" y="37.5"></rect>

I am trying to search it with following code:

WebDriverWait(driver, 1).until(ec.presence_of_element_located(("xpath", '//rect[@id="id123"]'))).click()

This does not work.

But the following does:

WebDriverWait(driver, 1).until(ec.presence_of_element_located(("xpath", '//*[name()="rect"][@id="id123"]'))).click()

Any clues on why the first one does not work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

<rect>

The <rect> element is a basic SVG shape that creates rectangles, defined by their corner's position, their width, and their height. The rectangles may have their corners rounded.

An Example:

<svg viewBox="0 0 220 100" xmlns="http://www.w3.org/2000/svg">
  <!-- Simple rect element -->
  <rect x="0" y="0" width="100" height="100" />

  <!-- Rounded corner rect element -->
  <rect x="120" y="0" width="100" height="100" rx="15" ry="15" />
</svg>

Attributes

The attributes of <rect> elements are as follows:

  • x: This attribute determines the x coordinate of the rect.
    • Value type: | ; Default value: 0; Animatable: yes
  • y: This attribute determines the y coordinate of the rect.
    • Value type: | ; Default value: 0; Animatable: yes
  • width: This attribute determines the width of the rect.
    • Value type: auto|| ; Default value: auto; Animatable: yes
  • height: This attribute determines the height of the rect.
    • Value type: auto|| ; Default value: auto; Animatable: yes
  • rx: This attribute determines the horizontal corner radius of the rect.
    • Value type: auto|| ; Default value: auto; Animatable: yes
  • ry: This attribute determines the vertical corner radius of the rect.
    • Value type: auto|| ; Default value: auto; Animatable: yes
  • pathLength: This attribute lets specify the total length for the path, in user units.
    • Value type: ; Default value: none; Animatable: yes

Note: Starting with SVG2 x, y, width, height, rx and ry are Geometry Properties, meaning those attributes can also be used as CSS properties for that element.


This usecase

As the <rect> element is a SVG element so to locate such elements you have to explicitly specify the SVG namespace when accessing the elements using as follows:

  • For <svg> elements:

    //*[name()="svg"]
    
  • For <g> elements:

    //*[name()="svg"]/*[name()="g"]
    
  • For <rect> elements:

    //*[name()="svg"]/*[name()="g"]/*[name()="rect"]
    //*[name()="svg"]/*[name()="rect"]
    

References

You can find a couple of relevant detailed discussions in


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...