0

Suppose i have a link such as

<a class="like_button" href="#" action_click="LikePost">I Like It</a>

How can i search the dom via pure JavaScript to scan the html for the action_click="LikePost" The code below doest not work for me

document.getElementsByTagName('action_click')

I feel though that a regular expression might needed.

2

3 Answers 3

3

Use document.querySelector

document.querySelector('[action_click="LikePost"]');

And if there are multiple such elements, use document.querySelectorAll which will return a live HTMLCollection.

document.querySelectorAll('[action_click="LikePost"]');
Sign up to request clarification or add additional context in comments.

Comments

1

You should use the querySelector() like that:

console.log(document.querySelector('[action_click="LikePost"]'));
<a class="like_button" href="#" action_click="LikePost">I Like It</a>

Comments

1

If you have only one element with that attribute then querySelector works.

var elem = document.querySelector('[action_click="LikePost"]');
console.log(elem);
<a class="like_button" href="#" action_click="LikePost">

But if you have multiple elements then querySelectorAll is what you need:

var elem = document.querySelectorAll('[action_click="LikePost"]');
console.log(elem);
<a class="like_button" href="#" action_click="LikePost">
<a class="like_button" href="#" action_click="LikePost">
<a class="like_button" href="#" action_click="LikePost">

1 Comment

Thanks for the answer, it helped! Im using this JavaScript code within a python script, but even if JavaScript syntax is correct, the script refuses to run complaining about a syntax error. driver.execute_script("window.a = document.querySelectorAll('[action_click="LikePost"]');") I think im missing something

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.