[WEB] Click on Link Using XPath [Example]

safari
webscrape
example
chrome
xpath
javascript

#21

Facebook Ads page, part where you launch campaigns n settings.

I want to share, but it contains private info.

But yah, notice FB always changes its Xpath 'id' for certain elements.


#22

You could copy the HTML, and change any private info to something made up, but of the same form.
I can't really help without seeing the HTML.


#23

Gmail also guilty of dynamic Xpath ids.

image

Trash button for example

On my side, its Xpath is showing this
//*[@id=":jl"]/div/div[2]

I'm sure it's different for everyone else.


#24

Ok, here's an example of the element in Facebook thingy.

<input class="_4b7k _4b7k_big _53rs" data-testid="custom-audience-name-input" placeholder="Name your audience" maxlength="50" value="" style="letter-spacing: normal; color: rgb(28, 30, 33); font-size: 12px; font-family: -apple-system, BlinkMacSystemFont, Arial, sans-serif; line-height: 16px; background: rgb(255, 255, 255); height: 26px; padding-bottom: 6px; padding-top: 6px; text-align: left;">

below is the Xpath-Absolute for it (right click Inspect > Copy Xpath)
//*[@id="facebook"]/body/div[4]/div[2]/div/div/div/div/div/div/div[2]/div/div/div[1]/div[2]/div/div/div/div[1]/div/label/input

so I gotta somehow study up XML n Xpath basics
so could write custom code
to select by either these 2 by Xpath-Attribute
(cause they're constant n don't change dynamically each time whatever)

  • data-testid="custom-audience-name-input"
  • placeholder="Name your audience"

cause from what I understand, the above Xpath is based on Xpath-Absolute/Position. Basically NOT a good way to define Xpath to use in KM, cause it can be dynamic n break

**UPDATE
oh hey, figured it out, Xpath-Attribute

//input[@data-testid='custom-audience-name-input' and @placeholder='Name your audience']

Works, awesome. Now gotta pray these 2 constants won't change in future.

Much thanks.

This helped explained

But wouldn't have been possible without your Xpath macro @JMichaelTX


#25

But stuck at this one

<input class="_4b7k _4b7k_big _53rs" data-testid="edit-name-field" placeholder="Enter your ad set name here..." value="TH - 18+" style="letter-spacing: normal; color: rgb(28, 30, 33); font-size: 12px; font-family: -apple-system, BlinkMacSystemFont, Arial, sans-serif; line-height: 16px; background: rgb(255, 255, 255); height: 26px; padding-bottom: 6px; padding-top: 6px; text-align: left;">

Xpath-Absolute
//*[@id="aymt-parent-switch-to-quick-creation-SCROLLING"]/div[2]/div[1]/div/div[2]/label/input

Xpath-Attribute? (not working)
//input[@data-testid='edit-name-field' and @placeholder='Enter your ad set name here...']

Xpath-Attribute? (not working)
//input[@class='_4b7k _4b7k_big _53rs' and @data-testid='edit-name-field' and @placeholder='Enter your ad set name here...']


#26

Hey @JMichaelTX

Could you please advise on how to modify the JavaScript to execute it in AppleScript.

I would like to set an XPath in JS instead of using KM action but not sure how exactly to make it work.

set java to "(function run (pXPathStr) {
  'use strict';

var xPathStr = document.kmvar.xPath;
  
var elemFound =  document.evaluate(
      xPathStr, document, null, 0, null
    ).iterateNext();
    
  if (elemFound) {
    elemFound.click();
  }
  else {
  }
  
  return returnResults;

})();

/* 
*/"

tell application "Safari" to tell the front document to set theText to do JavaScript java

UPDATE:
After spending hours i finally found a way to do it:

tell application "Safari"
	do JavaScript "path = '//*[@id=\"yw5\"]/li[7]/a'; 
theNode = document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
theNode.click();" in front document
end tell