ChatGPT select last paragraph

Hello,

I’m new here. I’m trying to automate some work with ChatGPT. I’m stuck in trying to copy the output.

The output is a paragraph element within a div with an even number (see below). The divs in between (with odd numbers) are my prompts (my questions for ChatGPT. I don’t want to copy those. I just want to select the last paragraph or triple click the last div. I want to run it in a loop. Everytime I add a new prompt and ChatGPT responds a new div is added with the next even number (10,12,14,16,17,18, etc.)

Here’s a YouTube video of someone doing something similar with a different RPA tool.

https://www.youtube.com/watch?v=4tQ_tktIHQE

Their tool, however, has a selector called ‘last paragraph’ that selects the last paragraph element. I didn’t find anything like that in UIvision. I also tried storeXpathCount but it’s too complicated for my skill level.

I can use XClick with #tripleclick to click in the text or the div to select the text but I don’t know how to put the newest p element or the newest div into a variable to use it as a selector. And especially how to do this in a loop.

These are examples of divs that are the output of prompts 5,6,7 and 8.

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[10]/div/div[2]/div[1]/div/div/p

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[12]/div/div[2]/div[1]/div/div/p

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[14]/div/div[2]/div[1]/div/div/p

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[16]/div/div[2]/div[1]/div/div/p

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[10]

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[12]

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[14]

//*[@id=“__next”]/div/div/main/div[1]/div/div/div/div[16]

So, I start ChatGPT and I ask

“how much is 2 plus 2?” <—this div is 1

ChatGPT reponds:

“2 plus 2 is equal to 4.” <—this is div 2

then I ask

“house in spanish” <—this div is 3

ChatGPT reponds:

"In Spanish, the word for “house” is “casa” <—this is div 4

So, I always want to copy the last output from ChatGPT.

Any ideas are appreciate!

The rule is easy: Whatever another RPA tool can do, UIV can do, too - but easier :slight_smile:

You were actually very close to the solution!

First we use to first use the CLICK command and the “Select” button get the raw XPath. Then, from the list of possible locators, select the easiest one. This video shows it:

Then, - and this was the missing step in your macro - we create a loop and increase the div[4] value by 2 each loop. That is all!

So here is the full ChatGPT web scraping automation with UI.Vision running:

Note that our automation is more robust than the Robomotion one you mentioned above. In the Robomotion video the person is using a hardcoded delay of 15 seconds to wait for the ChatGPT answer, but that will fail when the system is under load. We use computer vision (visualAssert) to wait for the “Regenerate” button to show. Only then we extract the text.

Source:

{
  "Name": "chatgpt",
  "CreationDate": "2022-12-23",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://chat.openai.com/chat",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "30",
      "Value": "!timeout_wait",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "diffcounter",
      "Description": ""
    },
    {
      "Command": "times",
      "Target": "10",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "tell me a random joke",
      "Value": "q",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${!times}: Question = ${q}",
      "Value": "blue",
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "xpath=//textarea",
      "Value": "${q}",
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "xpath=//form/div/div[2]/button",
      "Value": "",
      "Description": "button click"
    },
    {
      "Command": "visualAssert",
      "Target": "regen_button_dpi_168.png",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "click // xpath=//div[${diffcounter}]/div",
      "Value": "c",
      "Description": ""
    },
    {
      "Command": "storeText",
      "Target": "xpath=//div[4]/div",
      "Value": "testvar",
      "Description": "button click"
    },
    {
      "Command": "storeText",
      "Target": "xpath=//div[${diffcounter}]/div",
      "Value": "c",
      "Description": ""
    },
    {
      "Command": "echo",
      "Target": "${!times}: ChatGPT says: ${c}",
      "Value": "green",
      "Description": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return parseInt(${diffcounter}) +2 ;",
      "Value": "diffcounter",
      "Description": "Increment the variable by 2"
    },
    {
      "Command": "echo",
      "Target": "New Diffcounter: ${Diffcounter}",
      "Value": "pink",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

PS: ChatGPT is an amazing tool. :100:

1 Like

Thanks for this! It works great! I will try to customize it for my needs.

The only thing that did not work right away was the visualssert command. The only way I could solve it was by selecting a new screenshot of the ‘Regenerate’ button and I had to delete the dpi suffix for it to work. Even with my own screenshot, it was not working when I left the dpi suffix.

I am using it with google sheets right now so I have to switch windows, copy/paste, etc. so I figure working with CSV might be easier. But I’m not there yet.

I feel like I’m barely scratching the surface. I didn’t know the visualassert command and I’m amazed by it. I also discovered your YouTube channel which is super great! I have so much to watch ahead of me.

Thanks for your help. Any tips on the dpi issue? I noticed a note asking not to delete it.

I noticed that the first loop stores the prompt instead of the output for some reason. The loops after that work fine. I can’t understand why. Sometimes I want to run it not in a loop and in those times it would always store my own prompt and never the ChatGPT output.

What I did was using xpathcount to make sure that I always get the last div. I put an asterisk between the brackets and works well so far.

{
  "Command": "storeXpathCount",
  "Target": "xpath=//*[@id=\"__next\"]/div/div/main/div/div/div/div/div[*]/div",
  "Value": "xpath_count",
  "Description": ""
},

Your instructions were very valuable and showed me how to insert a variable in the div.

From your code, I did not understand how this works in loops:

{
  "Command": "store",
  "Target": "2",
  "Value": "diffcounter",
  "Description": ""
},

If I set diffcounter to 2 at the beginning of the macro, doesn’t it overwrite the following script on each loop? How does diffcounter value stay at the what the script set it to?

{return parseInt(${diffcounter}) +2 ;},

Also, I had to delete the line that sets the {times} variable because it was giving me an error about not being closed.

All in all, thanks so much for the assistance.