Python Forum
XPATH element does not change its value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XPATH element does not change its value
#1
Hi, all!

I am developing a robot that get into in a Website, do the logon and make somethings on that.

I put it in a loop using the for command, but, when it comes to run again, the previous elements found by XPATH, even changing the page, they reamin the same.

As a quickly explanation, the robot goes to a page with a lot of links and get the frist using TAB key. So, in the new page, it does a lot of things, and, calls the previous page again. The situation is, when the robot comes back to the previsous page and calls the, now, new frist link, the old page is displayed, with the previous actions did too.

And I am getting the link with TAB. But it seems that the TAB key does not work on the second time.

The most weird is that I nelease and nenew the XPATH variables that were used. So, it should not occurs.

The log below shows that the nav browser is using the same sessoin with a diferent element. We can see that the log register is on its second run, but that elements captured by the XPATH do not change.

I already made the
nav.refresh()
on nav variable, but it does not work too.

Can anyone help?

Runing for the 1 time!
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="ba67-415d-8c1b-0ee82cafbdec", element="4eeb-ada7-671b458b6bc8")>
Runing for the 2 time!
<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="ba67-415d-8c1b-0ee82cafbdec", element="a35d-49d0-8833-1adf485bd06f")>
Below is my almost entire code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
import random

nav = webdriver.Firefox(executable_path='/usr/local/lib/geckodriver')
nav.get('https://websiteurl')
nav.find_element_by_id('usuario').send_keys('user')
nav.find_element_by_id('senha').send_keys('password')
nav.find_element_by_id('btnEnviar').click()
nav.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div/div/div[3]/div/div[3]/form/button').click()

time.sleep(5)

for i in range(1,10):
    nav.get('https://webisteurl2')

    pergunta_xpath1, pergunta_xpath2, pergunta_xpath3, pergunta_xpath4 = "XPATH1", "XPATH2", "XPATH3", "XPATH4"
    print("Rodando pela ", i, " vez!", pergunta_xpath1, pergunta_xpath2, pergunta_xpath3, pergunta_xpath4)

    time.sleep(10)

    #nav.refresh()

    #time.sleep(10)

    actions = ActionChains(nav)
    actions.send_keys(Keys.TAB * 21)
    actions.send_keys(Keys.ENTER)
    actions.perform()

    time.sleep(10)

    try:
        nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p[1]')
    except NoSuchElementException:
        pergunta_xpath1 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/h2[1]')
    else:
        pergunta_xpath1 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p[1]')
        print(pergunta_xpath1, "\n")

    try:
        nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p[2]')
    except NoSuchElementException:
        pergunta_xpath2 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/h2[1]')
    else:
        pergunta_xpath2 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p[2]')
        print(pergunta_xpath2, "\n")

    try:
        nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p')
    except NoSuchElementException:
        pergunta_xpath3 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/h2[1]')
    else:
        pergunta_xpath3 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]/p')
        print(pergunta_xpath3, "\n")

    try:
        nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]')
    except NoSuchElementException:
        pergunta_xpath4 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/h2[1]')
    else:
        pergunta_xpath4 = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/span[1]')
        print(pergunta_xpath4)

    area_resposta = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/form/div/textarea')
    resposta = ""
    vai_finalizar = True
    resposta_encontrada = False
    responder_aluno = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/form/div/div[2]/button[1]')
    finalizar_atividade = nav.find_element_by_xpath('/html/body/div[1]/div/div[3]/div[1]/div/div[2]/div/div/form/div/div[2]/button[2]')

    actions = ActionChains(nav)
    actions.send_keys(Keys.TAB * 18)
    actions.perform()

    if (re.search("text",pergunta_xpath1.text) or re.search("text",pergunta_xpath2.text) \
            or re.search("text",pergunta_xpath3.text) or re.search("text",pergunta_xpath4.text)):
        resposta = resposta_comp_soc_etica01
        resposta_encontrada = True

    if resposta_encontrada and vai_finalizar:
        area_resposta.send_keys(resposta)
        finalizar_atividade.submit()
        time.sleep(20)
    elif resposta_encontrada and (vai_finalizar == False):
        area_resposta.send_keys(resposta)
        responder_aluno.submit()
        time.sleep(20)

Huh Huh Huh
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  can i get any xpath on click event? Prince_Bhatia 3 1,257 Apr-09-2019, 01:32 PM
Last Post: Larz60+
  Unable to locate element no such element gahhon 6 1,123 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Using xpath to get value of a nested element name using tag named xq1xq1xq1 3 2,458 Jan-18-2019, 07:13 PM
Last Post: Larz60+
  Xpath queries sairam132033 6 2,580 Dec-06-2017, 11:02 PM
Last Post: Larz60+
  change only one element of list to upper case python1980 3 7,179 Dec-03-2017, 07:23 PM
Last Post: python1980
  Change single element in 2D list changes every 1D element AceScottie 9 4,237 Nov-13-2017, 07:05 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020