-1

The objective is request Selenium to direct to a new page with an address that located under the sub-menu. According to the suggestion from OP1 and OP2, this activity can be achieved as follow;

sub_item_drop= WebDriverWait( self.browser, 20 ).until( EC.presence_of_element_located(
    (By.CSS_SELECTOR, "//a[href='/pguna/ambilduit/permainan.aspx’]") ) )
sub_item_drop.click()

Instead of redirecting to a new url, the compiler instead throw an error;

selenium.common.exceptions.TimeoutException: Message:

It seems to me that the compiler was unable to locate the CSS path given. May I know where did I do wrong? appreciate for any insight.

For easy troubleshooting, the complete outer HTML framework of the website is given below

<li id="tcl_SiringMenu1_sbmenu" class="has-sub">
    <a href="javascript:;">
     <b class="caret pull-right"></b>
     <i class=" tcl tcl -fw tcl -myr"></i>
     <span>Ruang PeluangGame <span class="badge pull-right bg-yellow m-l-4 m-r-4">90000</span> </span>
    </a>

    <ul class="sub-menu" style="display: none;">
        <li id="tcl_SiringMenu1_AmbilDuit">
        <a href="/pguna/ambilduit/permainan.aspx">
        Permainx LODR<span class="badge pull-right bg-green m-l-5 m-r-5">90000</span></a>
        </li>
    </ul>
</li>

Additional info

The full Xpath to the class="sub-menu"

/html/body/form/div[3]/div[2]/div/div[2]/div[2]/div[1]/ul[2]/li[5]/ul

The full Xpath to the class badge pull-right bg-green m-l-5 m-r-5 is

/html/body/form/div[3]/div[2]/div/div[2]/div[2]/div[1]/ul[2]/li[5]/ul/li/a/span

p.s., I am aware several technique using mouse hovering (e.g., OP3, OP4, OP5, OP6) to achieve similar objective, but the technique proposed in OP1 and OP2 look more compact and neat.

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
mpx
  • 3,081
  • 2
  • 26
  • 56
  • Change ```By.CSS_SELECTOR``` to ```By.XPATH``` and the xpath should be ```"//a[@href='/pguna/ambilduit/permainan.aspx']"``` – Dainius Preimantas Apr 22 '20 at 16:38
  • Thanks for the prompt reply @DainiusPreimantas, however the changes as suggested by you also give an error. Please see the Edit 1. – mpx Apr 22 '20 at 16:58
  • I see one solution to this error: https://stackoverflow.com/questions/44119081/how-do-you-fix-the-element-not-interactable-exception – Dainius Preimantas Apr 22 '20 at 16:59
  • Hi @DainiusPreimantas , there are 9 answers proposed there, may I know which are referring too? – mpx Apr 22 '20 at 17:09
  • I am referring to an answer from user EyuelDK. Basically this error occurs if the element is not loaded ( not visible ) or it is behind something. You have to wait for it to load or to move to that element and see what is wrong. – Dainius Preimantas Apr 22 '20 at 17:11
  • Hi Dainius, thanks for the direction to proper thread. But, I still not able to make it work. Please see my Edit 3. – mpx Apr 22 '20 at 17:28
  • 1
    What if we change ```EC.presence_of_element_located``` to ```EC.visibility_of_element_located```, then it will wait until the element is visible. – Dainius Preimantas Apr 22 '20 at 17:33

1 Answers1

2

Updated Solution:

Solution 1:

sub_item_drop= WebDriverWait(self.browser, 20 ).until( 
EC.element_to_be_clickable(
    (By.XPATH, "//a[@href='/pguna/ambilduit/permainan.aspx']") ) )
self.browser.execute_script("arguments[0].click();", sub_item_drop)

Solution 2:

    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//li[@id='tcl_SiringMenu1_sbmenu']//ul[@class='sub-menu']//a[@href='/pguna/ambilduit/permainan.aspx']")))
    self.browser.execute_script("arguments[0].click();", element)

Note : please add below imports to your solution

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
  • Hi @Dipak, thanks for the suggestion, however, compiler gave a timeout error as show in Edit 4 above. I have add the full path to both classes, maybe this can give new thought. – mpx Apr 23 '20 at 04:56
  • Can you provide me your link? – SeleniumUser Apr 23 '20 at 05:13
  • Unfortunately, the site require registration and login id. Nothing much I can provide except for the html structure. – mpx Apr 23 '20 at 06:12
  • Check updated solution and please dont update your comments in the question for what you have tried, You can use comments section below the answer for providing more information – SeleniumUser Apr 23 '20 at 07:13
  • Hi Dipak, thanks for the response. I still have not try this. But will update you soon! – mpx Apr 23 '20 at 14:19