click button with selenium

4

Hi, I'm new to Selenium.

This is the code of the button where I want to click

<a href="javascript:bp(26834)">Clic para ver</a>

I navigate to the indicated page but it does not reflect the click action that should show a hidden data. This the code that I use

login_attempt = browser.find_element_by_xpath("//*[@name='bt']")
login_attempt.click()

I would appreciate your help.

Greetings.

    
asked by Juan Ontivero 14.09.2018 в 23:46
source

2 answers

2

As @FJSevilla points out, the best option is to execute the command that you perform with javascript:bp(26834) , but for that you should wait for the page to load, the following code shows how to do it.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()

url = 'http://www.compraensanjuan.com/sitio/73744/ricardo-monte/9/1/1'
browser.get(url)

timeout = 3

try:
    myElem = WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.ID, 'bt')))
    browser.execute_script("bp(26834)")
except TimeoutException:
    print("Se vencio el timeout")

If you want to use xPath, a possible value is //a[contains(text(),'Clic para ver')] :

myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'bt')))
elem = browser.find_element_by_xpath("//a[contains(text(),'Clic para ver')]")
elem.click()
    
answered by 15.09.2018 / 02:09
source
0

The problem really is that you click on the div , not on the link it contains. You can select the <div> by id as you do now and then select the element <a> from it, for example using XPATH.

Later, if your ultimate goal is to get the phone as I suppose, you have to wait for the call to the JavaScript function to be processed. Since at this point the previous hyperlink disappears, we can use it with an wait implicit to know when the text is available with the phone.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException



timeout = 10
driver = webdriver.Firefox()
driver.get("http://www.compraensanjuan.com/sitio/73744/ricardo-monte/9/1/1")

try:
    wait = WebDriverWait(driver, timeout)
    div = wait.until(EC.presence_of_element_located((By.ID, 'bt')))
    wait = WebDriverWait(div, timeout)
    wait.until(EC.presence_of_element_located((By.XPATH, ".//a"))).click()
    wait.until(EC.invisibility_of_element_located((By.XPATH, './/a')))

except TimeoutException:
    print("Tiempo de espera superado")

else:
    print("El teléfono es {}".format(div.text))

driver.close()

You can directly select the element a using XPATH in several ways, for example with:

"//div[@id='bt']//a"

But since we need the div to get the phone at the end, we make up for it in two steps.

    
answered by 15.09.2018 в 04:37