Automatic verification of websites and web systems using Selenium.
I can't get this element by using find_element_by_xpath
!
I could take the element just before this ...!
Originally, there was a website that said, "For some reason, this site cannot be verified with Selenium."
Ew No way www The way of assembling the person who made it (no more) is bad www
Ask the members, "Would you like to reassemble?" Then, this word was returned.
I see. I threw it all, so let's see it in HTML.
** Everyone, let's add class and id attributes to HTML! ** **
… But it is nonsense to modify the screen to do “verification automation”. Website Although it is called a website, the content is a web system anyway. It could be php or JSP.
This time, I want to get the text of article 2 from sample.html
!
I would like to explain that it was a requirement.
sample.html
<html>
<head><!--abridgement--></head>
<body>
<div id='wrap'>
<div class='article'>
<article>
<h1>Article 1 title</h1>
<div>
<p>Body of article 1</p>
</div>
</article>
</div><!-- .article -->
<div class='article'>
<article>
<h1>Article 2 title</h1>
<div>
<p>Body of article 2</p>
</div>
</article>
</div><!-- .article -->
</div><!-- #wrap -->
</body>
</html>
Here is the specification method that was said to "do not work".
Doesn't work.py
path = "/html/body/div/div[2]/article/div"
elmt = driver.find_element_by_xpath(path)
It seems that you want to specify the div
in the second ʻarticle
of the div
(class = article) in the div
(id = wrap) in the body
in the html
.
Hmmmm. Isn't that "/ html / body / div [5]"?
I think it works.py
path = "/html/body/div[5]"
elmt = driver.find_element_by_xpath(path)
In the xpath specification, ** it doesn't matter how the screen elements are nested **.
** "How many times did the div appear from the top?" ** is the criterion for judgment.
Is it similar to the CSS pseudo-class : nth-child ()
?
I want to hide the second <div class ='article'>
Various things disappear.css
div#wrap div:nth-child(2) { display: none; }
If you specify, the "second div" in the first div # wrap
will be erased.
In the case of sample.html, the text of article 1 and article 2 are all hidden.
(Because it is a sample, specify the class! ... Don't say)
Hmm. Something is wrong. ..
After all, programmers are a race that lives by worrying about indentation and nesting, so it can be misunderstood, but I do not know what kind of structure the HTML is written in an external program. Whether it's CSS, Python, or Java, they don't move in the light of human will. Only the ** people ** who developed it know that "there are multiple articles and there are similar blocks" as in this case.
** When specifying an element with find_element_by_xpath, how many times does the element appear counting from the top? Please pay attention to **.
Recommended Posts