is there a way to click this "like" button on the "Like 4 Like" site? Because I can't do it via xpath or full xpath, not even using "webdriverwait". I always get the error Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='likebutton52335" } (Session info: chrome=100.0.4896.127)
even though I know it exists.
TEST = 0
while TEST < 1:
likebutton = driver1.page_source
time.sleep(10)
#BUTTON = re.findall('id="/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]"', likebutton, re.MULTILINE)
BUTTON = re.findall('id="likebutton(. ?)"><a', likebutton, re.MULTILINE)
if BUTTON == []:
TEST = 1
for num in BUTTON:
try:
button = 'likebutton%s' % num
ref = "//*[@id='%s']" % button
time.sleep(3)
#WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.XPATH, ref))).click()
#wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Like')]"))).click()
driver1.find_element(By.XPATH, ref).click()
#driver1.find_element(By.XPATH, "/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]").click()
time.sleep(2)
window_after = driver1.window_handles[1]
driver1.switch_to.window(window_after)
time.sleep(1)
likebutton = driver1.page_source
time.sleep(3)
CodePudding user response:
Your problem is inside your regex!
Actually your regex is configured to get:
id="(number"...)"<a
That explain this value for the regex in the error:
52335"
So you get other parameter from the HTML tag.
You need to change your regex by:
id="likebutton(. ?)"
Like that you get the content between doubles quotes.
Result:
TEST = 0
while TEST < 1:
likebutton = driver1.page_source
time.sleep(3)
// This Line was modified
BUTTON = re.findall('id="likebutton(. ?)"', likebutton, re.MULTILINE)
////////////////////////
if BUTTON == []:
TEST = 1
for num in BUTTON:
try:
button = 'likebutton%s' % num
ref = "//*[@id='%s']" % button
driver1.find_element(By.XPATH, ref).click()
time.sleep(2)
window_after = driver1.window_handles[1]
driver1.switch_to.window(window_after)
time.sleep(1)
likebutton = driver1.page_source
time.sleep(3)