Clicking a Button in Safari Web Page

I want to create a macros for automated filling the test questions here https://mooc2-ans.chaoxing.com/, I need to click the "Create new test" button.
i do: Activate Safari -> execute JavaScript in Safari {document.getElementsByClass('jb_btn jb_btn_104 fl fs14').click();}
this how the button looks like:
a href="javascript:;" onclick="creatWork();" class="jb_btn jb_btn_104 fl fs14">i class="PlusIcon"新建作业</a

And I works for "yandex.ru" but not here, I'm not familiar with JavaScript, if you know how to solve this problem, please help.

Hey Boris,

The link you provided is dead.

-Chris

Hi Chris, that's true cuz it requires registration, but I can copy and send all the necessary code for you. My question is how to click the button in safari apple events with JavaScript. I'm trying to do -> getElementByClass but but somehow it doesn't work. Is there any other option?

Personally I'd use QuerySelector.

-Chris

I agree with Chris @ccstone: use querySelector. You can do a Forum Search for a number of examples.

I have tried:
document.querySelector("div.bnt_group a[onclick='creatWork();']").click();
document.querySelector("div.bnt_group a[href='javascript\:;']").click();
document.querySelector("div.bnt_group a[href='javascript:;']").click();
document.querySelector(".jb_btn.jb_btn_104.fl.fs14").click();
document.querySelector('.jb_btn.jb_btn_104.fl.fs14' a[onclick='creatWork();']).click();
and honestly lots more, it works fine with another website, both and querySelector and getElementByClass, and ignores me here.

btw


it's <a class=''jb_btn jb_btn_104 fl fs14"...> here, with spaces. Other sites I try class doesn't contain spaces. So i put . instead of spaces but fail.

try this selector:

document.querySelector('a[onclick="creatWork();"]')

This works for me:

var btnElem = document.querySelector('div.fanyaTask div.bnt_group a.jb_btn');
btnElem.click()

based on an OCR of your HTML image, which could contain errors.

If this does not work, please post a broader range of HTML from your web page using Code Block.
Make sure the HTML contains enough UI elements so we can test run/view in a browser.

[Error] TypeError: null is not an object (evaluating 'ev.click')
Глобальный код (Элемент скрипта 5:10)
[Error] TypeError: null is not an object (evaluating 'btnElem.click')
Глобальный код (Элемент скрипта 6:10)

txt file with HTML attachedchaoxing.zip (60.1 KB)

I don't know what script you are using that causes that error, but when I try the JavaScript I gave you above, it works in the full script that you provided.
I did make some changes to the text display on this web page so I could clearly see which link was being clicked. I also replaced some Chinese text with English.

If I change to a querySelectorAll() it will find all occurrences of that element:

When I run this script:

var btnElemCol = document.querySelectorAll('div.fanyaTask div.bnt_group a.jb_btn');

//--- Click on First Link ---

if (btnElemCol) {
  var linkFound = 'First Link was found';
  btnElemCol[0].click()
} else {  var linkFound = 'First Link was found'; }

linkFound;

I get this:

Note that I made a change to the HTML to call a different function when that link is clicked:

                    <div class="bnt_group">
                        <a href="javascript:;" onclick="creatWorkTest();" class="jb_btn jb_btn_104 fl fs14"><i class="PlusIcon"><img src="/images/add.png"></i><h3>Link 1 -- Create Work Test</h3></a>
                            <a href="/work/library?courseid=216190566&amp;clazzid=35920106&amp;cpi=150304463" class="btnBlue btn_92 fl fs14"><h3>Link 2 -- Open Existing Work</h3></a>
                    </div>
                    <div class="clear"></div>
                </div>

        function creatWorkTest() {
            alert('"CreateWork link was clicked"');
            return;
        }

Here's the test Macro/script that I used:

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Click on First Button Link in chaoxig @bhohos Page [Example]

-~~~ VER: 1.0    2021-03-25 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Click on First Button Link in chaoxig @bhohos Page [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


I copied the code, executed it:

alert(btnElemCol[0]) - undefined.

I have no idea what exactly i do wrong, i am far from programming, but i am able to follow the instruction. Yet, i can do this action for another website but unable to do it for this one, what can go wrong?
iGCSE.webarchive.zip (442.8 KB)
I attach webArchive

No, you did NOT copy my code correctly.

Please try just running the test macro that I provided, without making any changes.

of course I first tried your script.Запись экрана 2021-03-29 в 08.39.04.zip (5.3 MB)
Add a video, I run the script, but still nothing.
I have a question, can I execute a function instead of clicking the instance?

If you ran my Macro as is, without any changes, and it does not work for you, then I have nothing else to offer. Maybe the language is interfering with our communications and/or the actual script.

Good luck.

Blockquote

thx!